blob: 6039db3dcf0269e0280c190968c6ec0abdfd0161 [file] [log] [blame]
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -08001/* 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>
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080010#include <linux/stacktrace.h>
11#include <linux/perf_event.h>
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080012#include "percpu_freelist.h"
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080013
Chenbo Feng4672ded2017-10-18 13:00:22 -070014#define STACK_CREATE_FLAG_MASK \
15 (BPF_F_RDONLY | BPF_F_WRONLY)
16
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080017struct stack_map_bucket {
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080018 struct pcpu_freelist_node fnode;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080019 u32 hash;
20 u32 nr;
21 u64 ip[];
22};
23
24struct bpf_stack_map {
25 struct bpf_map map;
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080026 void *elems;
27 struct pcpu_freelist freelist;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080028 u32 n_buckets;
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080029 struct stack_map_bucket *buckets[];
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080030};
31
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080032static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
33{
34 u32 elem_size = sizeof(struct stack_map_bucket) + smap->map.value_size;
35 int err;
36
Daniel Borkmann251d00b2017-01-18 15:14:17 +010037 smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080038 if (!smap->elems)
39 return -ENOMEM;
40
41 err = pcpu_freelist_init(&smap->freelist);
42 if (err)
43 goto free_elems;
44
45 pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
46 smap->map.max_entries);
47 return 0;
48
49free_elems:
Daniel Borkmann251d00b2017-01-18 15:14:17 +010050 bpf_map_area_free(smap->elems);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080051 return err;
52}
53
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080054/* Called from syscall */
55static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
56{
57 u32 value_size = attr->value_size;
58 struct bpf_stack_map *smap;
59 u64 cost, n_buckets;
60 int err;
61
62 if (!capable(CAP_SYS_ADMIN))
63 return ERR_PTR(-EPERM);
64
Chenbo Feng4672ded2017-10-18 13:00:22 -070065 if (attr->map_flags & ~STACK_CREATE_FLAG_MASK)
Alexei Starovoitov823707b2016-03-07 21:57:16 -080066 return ERR_PTR(-EINVAL);
67
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080068 /* check sanity of attributes */
69 if (attr->max_entries == 0 || attr->key_size != 4 ||
70 value_size < 8 || value_size % 8 ||
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -030071 value_size / 8 > sysctl_perf_event_max_stack)
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080072 return ERR_PTR(-EINVAL);
73
74 /* hash table size must be power of 2 */
75 n_buckets = roundup_pow_of_two(attr->max_entries);
76
77 cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
78 if (cost >= U32_MAX - PAGE_SIZE)
79 return ERR_PTR(-E2BIG);
80
Daniel Borkmann251d00b2017-01-18 15:14:17 +010081 smap = bpf_map_area_alloc(cost);
82 if (!smap)
83 return ERR_PTR(-ENOMEM);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080084
85 err = -E2BIG;
86 cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
87 if (cost >= U32_MAX - PAGE_SIZE)
88 goto free_smap;
89
90 smap->map.map_type = attr->map_type;
91 smap->map.key_size = attr->key_size;
92 smap->map.value_size = value_size;
93 smap->map.max_entries = attr->max_entries;
Daniel Borkmann816cfeb2018-03-08 16:17:32 +010094 smap->map.map_flags = attr->map_flags;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080095 smap->n_buckets = n_buckets;
96 smap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
97
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080098 err = bpf_map_precharge_memlock(smap->map.pages);
99 if (err)
100 goto free_smap;
101
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -0300102 err = get_callchain_buffers(sysctl_perf_event_max_stack);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800103 if (err)
104 goto free_smap;
105
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800106 err = prealloc_elems_and_freelist(smap);
107 if (err)
108 goto put_buffers;
109
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800110 return &smap->map;
111
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800112put_buffers:
113 put_callchain_buffers();
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800114free_smap:
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100115 bpf_map_area_free(smap);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800116 return ERR_PTR(err);
117}
118
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200119BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
120 u64, flags)
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800121{
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800122 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
123 struct perf_callchain_entry *trace;
124 struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
125 u32 max_depth = map->value_size / 8;
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -0300126 /* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
127 u32 init_nr = sysctl_perf_event_max_stack - max_depth;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800128 u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
129 u32 hash, id, trace_nr, trace_len;
130 bool user = flags & BPF_F_USER_STACK;
131 bool kernel = !user;
132 u64 *ips;
133
134 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
135 BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
136 return -EINVAL;
137
Arnaldo Carvalho de Melocfbcf462016-04-28 12:30:53 -0300138 trace = get_perf_callchain(regs, init_nr, kernel, user,
139 sysctl_perf_event_max_stack, false, false);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800140
141 if (unlikely(!trace))
142 /* couldn't fetch the stack trace */
143 return -EFAULT;
144
145 /* get_perf_callchain() guarantees that trace->nr >= init_nr
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -0300146 * and trace-nr <= sysctl_perf_event_max_stack, so trace_nr <= max_depth
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800147 */
148 trace_nr = trace->nr - init_nr;
149
150 if (trace_nr <= skip)
151 /* skipping more than usable stack trace */
152 return -EFAULT;
153
154 trace_nr -= skip;
155 trace_len = trace_nr * sizeof(u64);
156 ips = trace->ip + skip + init_nr;
157 hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
158 id = hash & (smap->n_buckets - 1);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800159 bucket = READ_ONCE(smap->buckets[id]);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800160
161 if (bucket && bucket->hash == hash) {
162 if (flags & BPF_F_FAST_STACK_CMP)
163 return id;
164 if (bucket->nr == trace_nr &&
165 memcmp(bucket->ip, ips, trace_len) == 0)
166 return id;
167 }
168
169 /* this call stack is not in the map, try to add it */
170 if (bucket && !(flags & BPF_F_REUSE_STACKID))
171 return -EEXIST;
172
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800173 new_bucket = (struct stack_map_bucket *)
174 pcpu_freelist_pop(&smap->freelist);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800175 if (unlikely(!new_bucket))
176 return -ENOMEM;
177
178 memcpy(new_bucket->ip, ips, trace_len);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800179 new_bucket->hash = hash;
180 new_bucket->nr = trace_nr;
181
182 old_bucket = xchg(&smap->buckets[id], new_bucket);
183 if (old_bucket)
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800184 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800185 return id;
186}
187
188const struct bpf_func_proto bpf_get_stackid_proto = {
189 .func = bpf_get_stackid,
190 .gpl_only = true,
191 .ret_type = RET_INTEGER,
192 .arg1_type = ARG_PTR_TO_CTX,
193 .arg2_type = ARG_CONST_MAP_PTR,
194 .arg3_type = ARG_ANYTHING,
195};
196
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800197/* Called from eBPF program */
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800198static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
199{
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800200 return NULL;
201}
202
203/* Called from syscall */
204int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
205{
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800206 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800207 struct stack_map_bucket *bucket, *old_bucket;
208 u32 id = *(u32 *)key, trace_len;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800209
210 if (unlikely(id >= smap->n_buckets))
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800211 return -ENOENT;
212
213 bucket = xchg(&smap->buckets[id], NULL);
214 if (!bucket)
215 return -ENOENT;
216
217 trace_len = bucket->nr * sizeof(u64);
218 memcpy(value, bucket->ip, trace_len);
219 memset(value + trace_len, 0, map->value_size - trace_len);
220
221 old_bucket = xchg(&smap->buckets[id], bucket);
222 if (old_bucket)
223 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
224 return 0;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800225}
226
227static int stack_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
228{
229 return -EINVAL;
230}
231
232static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
233 u64 map_flags)
234{
235 return -EINVAL;
236}
237
238/* Called from syscall or from eBPF program */
239static int stack_map_delete_elem(struct bpf_map *map, void *key)
240{
241 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
242 struct stack_map_bucket *old_bucket;
243 u32 id = *(u32 *)key;
244
245 if (unlikely(id >= smap->n_buckets))
246 return -E2BIG;
247
248 old_bucket = xchg(&smap->buckets[id], NULL);
249 if (old_bucket) {
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800250 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800251 return 0;
252 } else {
253 return -ENOENT;
254 }
255}
256
257/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
258static void stack_map_free(struct bpf_map *map)
259{
260 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800261
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800262 /* wait for bpf programs to complete before freeing stack map */
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800263 synchronize_rcu();
264
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100265 bpf_map_area_free(smap->elems);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800266 pcpu_freelist_destroy(&smap->freelist);
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100267 bpf_map_area_free(smap);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800268 put_callchain_buffers();
269}
270
271static const struct bpf_map_ops stack_map_ops = {
272 .map_alloc = stack_map_alloc,
273 .map_free = stack_map_free,
274 .map_get_next_key = stack_map_get_next_key,
275 .map_lookup_elem = stack_map_lookup_elem,
276 .map_update_elem = stack_map_update_elem,
277 .map_delete_elem = stack_map_delete_elem,
278};
279
280static struct bpf_map_type_list stack_map_type __read_mostly = {
281 .ops = &stack_map_ops,
282 .type = BPF_MAP_TYPE_STACK_TRACE,
283};
284
285static int __init register_stack_map(void)
286{
287 bpf_register_map_type(&stack_map_type);
288 return 0;
289}
290late_initcall(register_stack_map);