Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu
[linux-block.git] / kernel / bpf / stackmap.c
CommitLineData
d5a3b1f6
AS
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>
d5a3b1f6
AS
10#include <linux/stacktrace.h>
11#include <linux/perf_event.h>
557c0c6e 12#include "percpu_freelist.h"
d5a3b1f6
AS
13
14struct stack_map_bucket {
557c0c6e 15 struct pcpu_freelist_node fnode;
d5a3b1f6
AS
16 u32 hash;
17 u32 nr;
18 u64 ip[];
19};
20
21struct bpf_stack_map {
22 struct bpf_map map;
557c0c6e
AS
23 void *elems;
24 struct pcpu_freelist freelist;
d5a3b1f6 25 u32 n_buckets;
557c0c6e 26 struct stack_map_bucket *buckets[];
d5a3b1f6
AS
27};
28
557c0c6e
AS
29static 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
96eabe7a
MKL
34 smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
35 smap->map.numa_node);
557c0c6e
AS
36 if (!smap->elems)
37 return -ENOMEM;
38
39 err = pcpu_freelist_init(&smap->freelist);
40 if (err)
41 goto free_elems;
42
43 pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
44 smap->map.max_entries);
45 return 0;
46
47free_elems:
d407bd25 48 bpf_map_area_free(smap->elems);
557c0c6e
AS
49 return err;
50}
51
d5a3b1f6
AS
52/* Called from syscall */
53static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
54{
55 u32 value_size = attr->value_size;
56 struct bpf_stack_map *smap;
57 u64 cost, n_buckets;
58 int err;
59
60 if (!capable(CAP_SYS_ADMIN))
61 return ERR_PTR(-EPERM);
62
96eabe7a 63 if (attr->map_flags & ~BPF_F_NUMA_NODE)
823707b6
AS
64 return ERR_PTR(-EINVAL);
65
d5a3b1f6
AS
66 /* check sanity of attributes */
67 if (attr->max_entries == 0 || attr->key_size != 4 ||
68 value_size < 8 || value_size % 8 ||
c5dfd78e 69 value_size / 8 > sysctl_perf_event_max_stack)
d5a3b1f6
AS
70 return ERR_PTR(-EINVAL);
71
72 /* hash table size must be power of 2 */
73 n_buckets = roundup_pow_of_two(attr->max_entries);
74
75 cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
76 if (cost >= U32_MAX - PAGE_SIZE)
77 return ERR_PTR(-E2BIG);
78
96eabe7a 79 smap = bpf_map_area_alloc(cost, bpf_map_attr_numa_node(attr));
d407bd25
DB
80 if (!smap)
81 return ERR_PTR(-ENOMEM);
d5a3b1f6
AS
82
83 err = -E2BIG;
84 cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
85 if (cost >= U32_MAX - PAGE_SIZE)
86 goto free_smap;
87
88 smap->map.map_type = attr->map_type;
89 smap->map.key_size = attr->key_size;
90 smap->map.value_size = value_size;
91 smap->map.max_entries = attr->max_entries;
a316338c 92 smap->map.map_flags = attr->map_flags;
d5a3b1f6
AS
93 smap->n_buckets = n_buckets;
94 smap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
96eabe7a 95 smap->map.numa_node = bpf_map_attr_numa_node(attr);
d5a3b1f6 96
557c0c6e
AS
97 err = bpf_map_precharge_memlock(smap->map.pages);
98 if (err)
99 goto free_smap;
100
97c79a38 101 err = get_callchain_buffers(sysctl_perf_event_max_stack);
d5a3b1f6
AS
102 if (err)
103 goto free_smap;
104
557c0c6e
AS
105 err = prealloc_elems_and_freelist(smap);
106 if (err)
107 goto put_buffers;
108
d5a3b1f6
AS
109 return &smap->map;
110
557c0c6e
AS
111put_buffers:
112 put_callchain_buffers();
d5a3b1f6 113free_smap:
d407bd25 114 bpf_map_area_free(smap);
d5a3b1f6
AS
115 return ERR_PTR(err);
116}
117
f3694e00
DB
118BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
119 u64, flags)
d5a3b1f6 120{
d5a3b1f6
AS
121 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
122 struct perf_callchain_entry *trace;
123 struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
124 u32 max_depth = map->value_size / 8;
c5dfd78e
ACM
125 /* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
126 u32 init_nr = sysctl_perf_event_max_stack - max_depth;
d5a3b1f6
AS
127 u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
128 u32 hash, id, trace_nr, trace_len;
129 bool user = flags & BPF_F_USER_STACK;
130 bool kernel = !user;
131 u64 *ips;
132
133 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
134 BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
135 return -EINVAL;
136
cfbcf468
ACM
137 trace = get_perf_callchain(regs, init_nr, kernel, user,
138 sysctl_perf_event_max_stack, false, false);
d5a3b1f6
AS
139
140 if (unlikely(!trace))
141 /* couldn't fetch the stack trace */
142 return -EFAULT;
143
144 /* get_perf_callchain() guarantees that trace->nr >= init_nr
c5dfd78e 145 * and trace-nr <= sysctl_perf_event_max_stack, so trace_nr <= max_depth
d5a3b1f6
AS
146 */
147 trace_nr = trace->nr - init_nr;
148
149 if (trace_nr <= skip)
150 /* skipping more than usable stack trace */
151 return -EFAULT;
152
153 trace_nr -= skip;
154 trace_len = trace_nr * sizeof(u64);
155 ips = trace->ip + skip + init_nr;
156 hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
157 id = hash & (smap->n_buckets - 1);
557c0c6e 158 bucket = READ_ONCE(smap->buckets[id]);
d5a3b1f6
AS
159
160 if (bucket && bucket->hash == hash) {
161 if (flags & BPF_F_FAST_STACK_CMP)
162 return id;
163 if (bucket->nr == trace_nr &&
164 memcmp(bucket->ip, ips, trace_len) == 0)
165 return id;
166 }
167
168 /* this call stack is not in the map, try to add it */
169 if (bucket && !(flags & BPF_F_REUSE_STACKID))
170 return -EEXIST;
171
557c0c6e
AS
172 new_bucket = (struct stack_map_bucket *)
173 pcpu_freelist_pop(&smap->freelist);
d5a3b1f6
AS
174 if (unlikely(!new_bucket))
175 return -ENOMEM;
176
177 memcpy(new_bucket->ip, ips, trace_len);
d5a3b1f6
AS
178 new_bucket->hash = hash;
179 new_bucket->nr = trace_nr;
180
181 old_bucket = xchg(&smap->buckets[id], new_bucket);
182 if (old_bucket)
557c0c6e 183 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
d5a3b1f6
AS
184 return id;
185}
186
187const struct bpf_func_proto bpf_get_stackid_proto = {
188 .func = bpf_get_stackid,
189 .gpl_only = true,
190 .ret_type = RET_INTEGER,
191 .arg1_type = ARG_PTR_TO_CTX,
192 .arg2_type = ARG_CONST_MAP_PTR,
193 .arg3_type = ARG_ANYTHING,
194};
195
557c0c6e 196/* Called from eBPF program */
d5a3b1f6 197static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
557c0c6e
AS
198{
199 return NULL;
200}
201
202/* Called from syscall */
203int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
d5a3b1f6
AS
204{
205 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
557c0c6e
AS
206 struct stack_map_bucket *bucket, *old_bucket;
207 u32 id = *(u32 *)key, trace_len;
d5a3b1f6
AS
208
209 if (unlikely(id >= smap->n_buckets))
557c0c6e
AS
210 return -ENOENT;
211
212 bucket = xchg(&smap->buckets[id], NULL);
213 if (!bucket)
214 return -ENOENT;
215
216 trace_len = bucket->nr * sizeof(u64);
217 memcpy(value, bucket->ip, trace_len);
218 memset(value + trace_len, 0, map->value_size - trace_len);
219
220 old_bucket = xchg(&smap->buckets[id], bucket);
221 if (old_bucket)
222 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
223 return 0;
d5a3b1f6
AS
224}
225
226static int stack_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
227{
228 return -EINVAL;
229}
230
231static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
232 u64 map_flags)
233{
234 return -EINVAL;
235}
236
237/* Called from syscall or from eBPF program */
238static int stack_map_delete_elem(struct bpf_map *map, void *key)
239{
240 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
241 struct stack_map_bucket *old_bucket;
242 u32 id = *(u32 *)key;
243
244 if (unlikely(id >= smap->n_buckets))
245 return -E2BIG;
246
247 old_bucket = xchg(&smap->buckets[id], NULL);
248 if (old_bucket) {
557c0c6e 249 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
d5a3b1f6
AS
250 return 0;
251 } else {
252 return -ENOENT;
253 }
254}
255
256/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
257static void stack_map_free(struct bpf_map *map)
258{
259 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
d5a3b1f6 260
557c0c6e 261 /* wait for bpf programs to complete before freeing stack map */
d5a3b1f6
AS
262 synchronize_rcu();
263
d407bd25 264 bpf_map_area_free(smap->elems);
557c0c6e 265 pcpu_freelist_destroy(&smap->freelist);
d407bd25 266 bpf_map_area_free(smap);
d5a3b1f6
AS
267 put_callchain_buffers();
268}
269
40077e0c 270const struct bpf_map_ops stack_map_ops = {
d5a3b1f6
AS
271 .map_alloc = stack_map_alloc,
272 .map_free = stack_map_free,
273 .map_get_next_key = stack_map_get_next_key,
274 .map_lookup_elem = stack_map_lookup_elem,
275 .map_update_elem = stack_map_update_elem,
276 .map_delete_elem = stack_map_delete_elem,
277};