blob: a2a232dec2363137c0b30851fda9106dd465f48d [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
14struct stack_map_bucket {
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080015 struct pcpu_freelist_node fnode;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080016 u32 hash;
17 u32 nr;
18 u64 ip[];
19};
20
21struct bpf_stack_map {
22 struct bpf_map map;
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080023 void *elems;
24 struct pcpu_freelist freelist;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080025 u32 n_buckets;
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080026 struct stack_map_bucket *buckets[];
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080027};
28
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080029static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
30{
31 u32 elem_size = sizeof(struct stack_map_bucket) + smap->map.value_size;
32 int err;
33
Daniel Borkmann251d00b2017-01-18 15:14:17 +010034 smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080035 if (!smap->elems)
36 return -ENOMEM;
37
38 err = pcpu_freelist_init(&smap->freelist);
39 if (err)
40 goto free_elems;
41
42 pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
43 smap->map.max_entries);
44 return 0;
45
46free_elems:
Daniel Borkmann251d00b2017-01-18 15:14:17 +010047 bpf_map_area_free(smap->elems);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080048 return err;
49}
50
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080051/* Called from syscall */
52static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
53{
54 u32 value_size = attr->value_size;
55 struct bpf_stack_map *smap;
56 u64 cost, n_buckets;
57 int err;
58
59 if (!capable(CAP_SYS_ADMIN))
60 return ERR_PTR(-EPERM);
61
Alexei Starovoitov823707b2016-03-07 21:57:16 -080062 if (attr->map_flags)
63 return ERR_PTR(-EINVAL);
64
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080065 /* check sanity of attributes */
66 if (attr->max_entries == 0 || attr->key_size != 4 ||
67 value_size < 8 || value_size % 8 ||
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -030068 value_size / 8 > sysctl_perf_event_max_stack)
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080069 return ERR_PTR(-EINVAL);
70
71 /* hash table size must be power of 2 */
72 n_buckets = roundup_pow_of_two(attr->max_entries);
73
74 cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
75 if (cost >= U32_MAX - PAGE_SIZE)
76 return ERR_PTR(-E2BIG);
77
Daniel Borkmann251d00b2017-01-18 15:14:17 +010078 smap = bpf_map_area_alloc(cost);
79 if (!smap)
80 return ERR_PTR(-ENOMEM);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080081
82 err = -E2BIG;
83 cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
84 if (cost >= U32_MAX - PAGE_SIZE)
85 goto free_smap;
86
87 smap->map.map_type = attr->map_type;
88 smap->map.key_size = attr->key_size;
89 smap->map.value_size = value_size;
90 smap->map.max_entries = attr->max_entries;
Daniel Borkmann816cfeb2018-03-08 16:17:32 +010091 smap->map.map_flags = attr->map_flags;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -080092 smap->n_buckets = n_buckets;
93 smap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
94
Alexei Starovoitov557c0c62016-03-07 21:57:17 -080095 err = bpf_map_precharge_memlock(smap->map.pages);
96 if (err)
97 goto free_smap;
98
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -030099 err = get_callchain_buffers(sysctl_perf_event_max_stack);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800100 if (err)
101 goto free_smap;
102
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800103 err = prealloc_elems_and_freelist(smap);
104 if (err)
105 goto put_buffers;
106
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800107 return &smap->map;
108
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800109put_buffers:
110 put_callchain_buffers();
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800111free_smap:
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100112 bpf_map_area_free(smap);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800113 return ERR_PTR(err);
114}
115
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200116BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
117 u64, flags)
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800118{
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800119 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
120 struct perf_callchain_entry *trace;
121 struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
122 u32 max_depth = map->value_size / 8;
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -0300123 /* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
124 u32 init_nr = sysctl_perf_event_max_stack - max_depth;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800125 u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
126 u32 hash, id, trace_nr, trace_len;
127 bool user = flags & BPF_F_USER_STACK;
128 bool kernel = !user;
129 u64 *ips;
130
131 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
132 BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
133 return -EINVAL;
134
Arnaldo Carvalho de Melocfbcf462016-04-28 12:30:53 -0300135 trace = get_perf_callchain(regs, init_nr, kernel, user,
136 sysctl_perf_event_max_stack, false, false);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800137
138 if (unlikely(!trace))
139 /* couldn't fetch the stack trace */
140 return -EFAULT;
141
142 /* get_perf_callchain() guarantees that trace->nr >= init_nr
Arnaldo Carvalho de Meloc5dfd782016-04-21 12:28:50 -0300143 * and trace-nr <= sysctl_perf_event_max_stack, so trace_nr <= max_depth
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800144 */
145 trace_nr = trace->nr - init_nr;
146
147 if (trace_nr <= skip)
148 /* skipping more than usable stack trace */
149 return -EFAULT;
150
151 trace_nr -= skip;
152 trace_len = trace_nr * sizeof(u64);
153 ips = trace->ip + skip + init_nr;
154 hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
155 id = hash & (smap->n_buckets - 1);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800156 bucket = READ_ONCE(smap->buckets[id]);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800157
158 if (bucket && bucket->hash == hash) {
159 if (flags & BPF_F_FAST_STACK_CMP)
160 return id;
161 if (bucket->nr == trace_nr &&
162 memcmp(bucket->ip, ips, trace_len) == 0)
163 return id;
164 }
165
166 /* this call stack is not in the map, try to add it */
167 if (bucket && !(flags & BPF_F_REUSE_STACKID))
168 return -EEXIST;
169
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800170 new_bucket = (struct stack_map_bucket *)
171 pcpu_freelist_pop(&smap->freelist);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800172 if (unlikely(!new_bucket))
173 return -ENOMEM;
174
175 memcpy(new_bucket->ip, ips, trace_len);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800176 new_bucket->hash = hash;
177 new_bucket->nr = trace_nr;
178
179 old_bucket = xchg(&smap->buckets[id], new_bucket);
180 if (old_bucket)
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800181 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800182 return id;
183}
184
185const struct bpf_func_proto bpf_get_stackid_proto = {
186 .func = bpf_get_stackid,
187 .gpl_only = true,
188 .ret_type = RET_INTEGER,
189 .arg1_type = ARG_PTR_TO_CTX,
190 .arg2_type = ARG_CONST_MAP_PTR,
191 .arg3_type = ARG_ANYTHING,
192};
193
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800194/* Called from eBPF program */
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800195static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
196{
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800197 return NULL;
198}
199
200/* Called from syscall */
201int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
202{
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800203 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800204 struct stack_map_bucket *bucket, *old_bucket;
205 u32 id = *(u32 *)key, trace_len;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800206
207 if (unlikely(id >= smap->n_buckets))
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800208 return -ENOENT;
209
210 bucket = xchg(&smap->buckets[id], NULL);
211 if (!bucket)
212 return -ENOENT;
213
214 trace_len = bucket->nr * sizeof(u64);
215 memcpy(value, bucket->ip, trace_len);
216 memset(value + trace_len, 0, map->value_size - trace_len);
217
218 old_bucket = xchg(&smap->buckets[id], bucket);
219 if (old_bucket)
220 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
221 return 0;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800222}
223
224static int stack_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
225{
226 return -EINVAL;
227}
228
229static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
230 u64 map_flags)
231{
232 return -EINVAL;
233}
234
235/* Called from syscall or from eBPF program */
236static int stack_map_delete_elem(struct bpf_map *map, void *key)
237{
238 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
239 struct stack_map_bucket *old_bucket;
240 u32 id = *(u32 *)key;
241
242 if (unlikely(id >= smap->n_buckets))
243 return -E2BIG;
244
245 old_bucket = xchg(&smap->buckets[id], NULL);
246 if (old_bucket) {
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800247 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800248 return 0;
249 } else {
250 return -ENOENT;
251 }
252}
253
254/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
255static void stack_map_free(struct bpf_map *map)
256{
257 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800258
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800259 /* wait for bpf programs to complete before freeing stack map */
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800260 synchronize_rcu();
261
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100262 bpf_map_area_free(smap->elems);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800263 pcpu_freelist_destroy(&smap->freelist);
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100264 bpf_map_area_free(smap);
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800265 put_callchain_buffers();
266}
267
268static const struct bpf_map_ops stack_map_ops = {
269 .map_alloc = stack_map_alloc,
270 .map_free = stack_map_free,
271 .map_get_next_key = stack_map_get_next_key,
272 .map_lookup_elem = stack_map_lookup_elem,
273 .map_update_elem = stack_map_update_elem,
274 .map_delete_elem = stack_map_delete_elem,
275};
276
277static struct bpf_map_type_list stack_map_type __read_mostly = {
278 .ops = &stack_map_ops,
279 .type = BPF_MAP_TYPE_STACK_TRACE,
280};
281
282static int __init register_stack_map(void)
283{
284 bpf_register_map_type(&stack_map_type);
285 return 0;
286}
287late_initcall(register_stack_map);