blob: 9a1e6ed7babce6a9fbaf4126b6fd2f04d95eee9d [file] [log] [blame]
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -08001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
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 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 */
12#include <linux/bpf.h>
13#include <linux/err.h>
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080014#include <linux/slab.h>
15#include <linux/mm.h>
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -070016#include <linux/filter.h>
Daniel Borkmann0cdf56402015-10-02 18:42:00 +020017#include <linux/perf_event.h>
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080018
Alexei Starovoitova10423b2016-02-01 22:39:54 -080019static void bpf_array_free_percpu(struct bpf_array *array)
20{
21 int i;
22
23 for (i = 0; i < array->map.max_entries; i++)
24 free_percpu(array->pptrs[i]);
25}
26
27static int bpf_array_alloc_percpu(struct bpf_array *array)
28{
29 void __percpu *ptr;
30 int i;
31
32 for (i = 0; i < array->map.max_entries; i++) {
33 ptr = __alloc_percpu_gfp(array->elem_size, 8,
34 GFP_USER | __GFP_NOWARN);
35 if (!ptr) {
36 bpf_array_free_percpu(array);
37 return -ENOMEM;
38 }
39 array->pptrs[i] = ptr;
40 }
41
42 return 0;
43}
44
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080045/* Called from syscall */
46static struct bpf_map *array_map_alloc(union bpf_attr *attr)
47{
Alexei Starovoitova10423b2016-02-01 22:39:54 -080048 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080049 u32 elem_size, index_mask, max_entries;
50 bool unpriv = !capable(CAP_SYS_ADMIN);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080051 struct bpf_array *array;
Daniel Borkmann820ef2a2018-01-10 23:25:05 +010052 u64 array_size, mask64;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080053
54 /* check sanity of attributes */
55 if (attr->max_entries == 0 || attr->key_size != 4 ||
Alexei Starovoitov823707b2016-03-07 21:57:16 -080056 attr->value_size == 0 || attr->map_flags)
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080057 return ERR_PTR(-EINVAL);
58
Alexei Starovoitov01b3f522015-11-29 16:59:35 -080059 if (attr->value_size >= 1 << (KMALLOC_SHIFT_MAX - 1))
60 /* if value_size is bigger, the user space won't be able to
61 * access the elements.
62 */
63 return ERR_PTR(-E2BIG);
64
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080065 elem_size = round_up(attr->value_size, 8);
66
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080067 max_entries = attr->max_entries;
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080068
Daniel Borkmann820ef2a2018-01-10 23:25:05 +010069 /* On 32 bit archs roundup_pow_of_two() with max_entries that has
70 * upper most bit set in u32 space is undefined behavior due to
71 * resulting 1U << 32, so do it manually here in u64 space.
72 */
73 mask64 = fls_long(max_entries - 1);
74 mask64 = 1ULL << mask64;
75 mask64 -= 1;
76
77 index_mask = mask64;
78 if (unpriv) {
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080079 /* round up array size to nearest power of 2,
80 * since cpu will speculate within index_mask limits
81 */
82 max_entries = index_mask + 1;
Daniel Borkmann820ef2a2018-01-10 23:25:05 +010083 /* Check for overflows. */
84 if (max_entries < attr->max_entries)
85 return ERR_PTR(-E2BIG);
86 }
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080087
Alexei Starovoitova10423b2016-02-01 22:39:54 -080088 array_size = sizeof(*array);
89 if (percpu)
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080090 array_size += (u64) max_entries * sizeof(void *);
Alexei Starovoitova10423b2016-02-01 22:39:54 -080091 else
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080092 array_size += (u64) max_entries * elem_size;
Alexei Starovoitova10423b2016-02-01 22:39:54 -080093
94 /* make sure there is no u32 overflow later in round_up() */
95 if (array_size >= U32_MAX - PAGE_SIZE)
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -080096 return ERR_PTR(-ENOMEM);
97
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080098 /* allocate all map elements and zero-initialize them */
Daniel Borkmann251d00b2017-01-18 15:14:17 +010099 array = bpf_map_area_alloc(array_size);
100 if (!array)
101 return ERR_PTR(-ENOMEM);
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800102 array->index_mask = index_mask;
103 array->map.unpriv_array = unpriv;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800104
105 /* copy mandatory map attributes */
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800106 array->map.map_type = attr->map_type;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800107 array->map.key_size = attr->key_size;
108 array->map.value_size = attr->value_size;
109 array->map.max_entries = attr->max_entries;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800110 array->elem_size = elem_size;
111
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800112 if (!percpu)
113 goto out;
114
115 array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
116
117 if (array_size >= U32_MAX - PAGE_SIZE ||
118 elem_size > PCPU_MIN_UNIT_SIZE || bpf_array_alloc_percpu(array)) {
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100119 bpf_map_area_free(array);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800120 return ERR_PTR(-ENOMEM);
121 }
122out:
123 array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
124
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800125 return &array->map;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800126}
127
128/* Called from syscall or from eBPF program */
129static void *array_map_lookup_elem(struct bpf_map *map, void *key)
130{
131 struct bpf_array *array = container_of(map, struct bpf_array, map);
132 u32 index = *(u32 *)key;
133
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800134 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800135 return NULL;
136
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800137 return array->value + array->elem_size * (index & array->index_mask);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800138}
139
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800140/* Called from eBPF program */
141static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
142{
143 struct bpf_array *array = container_of(map, struct bpf_array, map);
144 u32 index = *(u32 *)key;
145
146 if (unlikely(index >= array->map.max_entries))
147 return NULL;
148
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800149 return this_cpu_ptr(array->pptrs[index & array->index_mask]);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800150}
151
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800152int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
153{
154 struct bpf_array *array = container_of(map, struct bpf_array, map);
155 u32 index = *(u32 *)key;
156 void __percpu *pptr;
157 int cpu, off = 0;
158 u32 size;
159
160 if (unlikely(index >= array->map.max_entries))
161 return -ENOENT;
162
163 /* per_cpu areas are zero-filled and bpf programs can only
164 * access 'value_size' of them, so copying rounded areas
165 * will not leak any kernel data
166 */
167 size = round_up(map->value_size, 8);
168 rcu_read_lock();
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800169 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800170 for_each_possible_cpu(cpu) {
171 bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
172 off += size;
173 }
174 rcu_read_unlock();
175 return 0;
176}
177
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800178/* Called from syscall */
179static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
180{
181 struct bpf_array *array = container_of(map, struct bpf_array, map);
182 u32 index = *(u32 *)key;
183 u32 *next = (u32 *)next_key;
184
185 if (index >= array->map.max_entries) {
186 *next = 0;
187 return 0;
188 }
189
190 if (index == array->map.max_entries - 1)
191 return -ENOENT;
192
193 *next = index + 1;
194 return 0;
195}
196
197/* Called from syscall or from eBPF program */
198static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
199 u64 map_flags)
200{
201 struct bpf_array *array = container_of(map, struct bpf_array, map);
202 u32 index = *(u32 *)key;
203
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800204 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800205 /* unknown flags */
206 return -EINVAL;
207
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800208 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800209 /* all elements were pre-allocated, cannot insert a new one */
210 return -E2BIG;
211
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800212 if (unlikely(map_flags == BPF_NOEXIST))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800213 /* all elements already exist */
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800214 return -EEXIST;
215
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800216 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800217 memcpy(this_cpu_ptr(array->pptrs[index & array->index_mask]),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800218 value, map->value_size);
219 else
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800220 memcpy(array->value +
221 array->elem_size * (index & array->index_mask),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800222 value, map->value_size);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800223 return 0;
224}
225
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800226int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
227 u64 map_flags)
228{
229 struct bpf_array *array = container_of(map, struct bpf_array, map);
230 u32 index = *(u32 *)key;
231 void __percpu *pptr;
232 int cpu, off = 0;
233 u32 size;
234
235 if (unlikely(map_flags > BPF_EXIST))
236 /* unknown flags */
237 return -EINVAL;
238
239 if (unlikely(index >= array->map.max_entries))
240 /* all elements were pre-allocated, cannot insert a new one */
241 return -E2BIG;
242
243 if (unlikely(map_flags == BPF_NOEXIST))
244 /* all elements already exist */
245 return -EEXIST;
246
247 /* the user space will provide round_up(value_size, 8) bytes that
248 * will be copied into per-cpu area. bpf programs can only access
249 * value_size of it. During lookup the same extra bytes will be
250 * returned or zeros which were zero-filled by percpu_alloc,
251 * so no kernel data leaks possible
252 */
253 size = round_up(map->value_size, 8);
254 rcu_read_lock();
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800255 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800256 for_each_possible_cpu(cpu) {
257 bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
258 off += size;
259 }
260 rcu_read_unlock();
261 return 0;
262}
263
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800264/* Called from syscall or from eBPF program */
265static int array_map_delete_elem(struct bpf_map *map, void *key)
266{
267 return -EINVAL;
268}
269
270/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
271static void array_map_free(struct bpf_map *map)
272{
273 struct bpf_array *array = container_of(map, struct bpf_array, map);
274
275 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
276 * so the programs (can be more than one that used this map) were
277 * disconnected from events. Wait for outstanding programs to complete
278 * and free the array
279 */
280 synchronize_rcu();
281
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800282 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
283 bpf_array_free_percpu(array);
284
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100285 bpf_map_area_free(array);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800286}
287
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100288static const struct bpf_map_ops array_ops = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800289 .map_alloc = array_map_alloc,
290 .map_free = array_map_free,
291 .map_get_next_key = array_map_get_next_key,
292 .map_lookup_elem = array_map_lookup_elem,
293 .map_update_elem = array_map_update_elem,
294 .map_delete_elem = array_map_delete_elem,
295};
296
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100297static struct bpf_map_type_list array_type __read_mostly = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800298 .ops = &array_ops,
299 .type = BPF_MAP_TYPE_ARRAY,
300};
301
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800302static const struct bpf_map_ops percpu_array_ops = {
303 .map_alloc = array_map_alloc,
304 .map_free = array_map_free,
305 .map_get_next_key = array_map_get_next_key,
306 .map_lookup_elem = percpu_array_map_lookup_elem,
307 .map_update_elem = array_map_update_elem,
308 .map_delete_elem = array_map_delete_elem,
309};
310
311static struct bpf_map_type_list percpu_array_type __read_mostly = {
312 .ops = &percpu_array_ops,
313 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
314};
315
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800316static int __init register_array_map(void)
317{
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100318 bpf_register_map_type(&array_type);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800319 bpf_register_map_type(&percpu_array_type);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800320 return 0;
321}
322late_initcall(register_array_map);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700323
Wang Nan2a36f0b2015-08-06 07:02:33 +0000324static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700325{
Wang Nan2a36f0b2015-08-06 07:02:33 +0000326 /* only file descriptors can be stored in this type of map */
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700327 if (attr->value_size != sizeof(u32))
328 return ERR_PTR(-EINVAL);
329 return array_map_alloc(attr);
330}
331
Wang Nan2a36f0b2015-08-06 07:02:33 +0000332static void fd_array_map_free(struct bpf_map *map)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700333{
334 struct bpf_array *array = container_of(map, struct bpf_array, map);
335 int i;
336
337 synchronize_rcu();
338
339 /* make sure it's empty */
340 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000341 BUG_ON(array->ptrs[i] != NULL);
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100342
343 bpf_map_area_free(array);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700344}
345
Wang Nan2a36f0b2015-08-06 07:02:33 +0000346static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700347{
348 return NULL;
349}
350
351/* only called from syscall */
Daniel Borkmannd056a782016-06-15 22:47:13 +0200352int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
353 void *key, void *value, u64 map_flags)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700354{
355 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000356 void *new_ptr, *old_ptr;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700357 u32 index = *(u32 *)key, ufd;
358
359 if (map_flags != BPF_ANY)
360 return -EINVAL;
361
362 if (index >= array->map.max_entries)
363 return -E2BIG;
364
365 ufd = *(u32 *)value;
Daniel Borkmannd056a782016-06-15 22:47:13 +0200366 new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000367 if (IS_ERR(new_ptr))
368 return PTR_ERR(new_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700369
Wang Nan2a36f0b2015-08-06 07:02:33 +0000370 old_ptr = xchg(array->ptrs + index, new_ptr);
371 if (old_ptr)
372 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700373
374 return 0;
375}
376
Wang Nan2a36f0b2015-08-06 07:02:33 +0000377static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700378{
379 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000380 void *old_ptr;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700381 u32 index = *(u32 *)key;
382
383 if (index >= array->map.max_entries)
384 return -E2BIG;
385
Wang Nan2a36f0b2015-08-06 07:02:33 +0000386 old_ptr = xchg(array->ptrs + index, NULL);
387 if (old_ptr) {
388 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700389 return 0;
390 } else {
391 return -ENOENT;
392 }
393}
394
Daniel Borkmannd056a782016-06-15 22:47:13 +0200395static void *prog_fd_array_get_ptr(struct bpf_map *map,
396 struct file *map_file, int fd)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000397{
398 struct bpf_array *array = container_of(map, struct bpf_array, map);
399 struct bpf_prog *prog = bpf_prog_get(fd);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200400
Wang Nan2a36f0b2015-08-06 07:02:33 +0000401 if (IS_ERR(prog))
402 return prog;
403
404 if (!bpf_prog_array_compatible(array, prog)) {
405 bpf_prog_put(prog);
406 return ERR_PTR(-EINVAL);
407 }
Daniel Borkmannd056a782016-06-15 22:47:13 +0200408
Wang Nan2a36f0b2015-08-06 07:02:33 +0000409 return prog;
410}
411
412static void prog_fd_array_put_ptr(void *ptr)
413{
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200414 bpf_prog_put(ptr);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000415}
416
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700417/* decrement refcnt of all bpf_progs that are stored in this map */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000418void bpf_fd_array_map_clear(struct bpf_map *map)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700419{
420 struct bpf_array *array = container_of(map, struct bpf_array, map);
421 int i;
422
423 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000424 fd_array_map_delete_elem(map, &i);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700425}
426
427static const struct bpf_map_ops prog_array_ops = {
Wang Nan2a36f0b2015-08-06 07:02:33 +0000428 .map_alloc = fd_array_map_alloc,
429 .map_free = fd_array_map_free,
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700430 .map_get_next_key = array_map_get_next_key,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000431 .map_lookup_elem = fd_array_map_lookup_elem,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000432 .map_delete_elem = fd_array_map_delete_elem,
433 .map_fd_get_ptr = prog_fd_array_get_ptr,
434 .map_fd_put_ptr = prog_fd_array_put_ptr,
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700435};
436
437static struct bpf_map_type_list prog_array_type __read_mostly = {
438 .ops = &prog_array_ops,
439 .type = BPF_MAP_TYPE_PROG_ARRAY,
440};
441
442static int __init register_prog_array_map(void)
443{
444 bpf_register_map_type(&prog_array_type);
445 return 0;
446}
447late_initcall(register_prog_array_map);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000448
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200449static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
450 struct file *map_file)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000451{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200452 struct bpf_event_entry *ee;
453
Daniel Borkmann858d68f2016-07-16 01:15:55 +0200454 ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200455 if (ee) {
456 ee->event = perf_file->private_data;
457 ee->perf_file = perf_file;
458 ee->map_file = map_file;
459 }
460
461 return ee;
462}
463
464static void __bpf_event_entry_free(struct rcu_head *rcu)
465{
466 struct bpf_event_entry *ee;
467
468 ee = container_of(rcu, struct bpf_event_entry, rcu);
469 fput(ee->perf_file);
470 kfree(ee);
471}
472
473static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
474{
475 call_rcu(&ee->rcu, __bpf_event_entry_free);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000476}
477
Daniel Borkmannd056a782016-06-15 22:47:13 +0200478static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
479 struct file *map_file, int fd)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000480{
Kaixu Xiaea317b22015-08-06 07:02:34 +0000481 const struct perf_event_attr *attr;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200482 struct bpf_event_entry *ee;
483 struct perf_event *event;
484 struct file *perf_file;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000485
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200486 perf_file = perf_event_get(fd);
487 if (IS_ERR(perf_file))
488 return perf_file;
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -0800489
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200490 event = perf_file->private_data;
491 ee = ERR_PTR(-EINVAL);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000492
493 attr = perf_event_attrs(event);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200494 if (IS_ERR(attr) || attr->inherit)
495 goto err_out;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000496
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200497 switch (attr->type) {
498 case PERF_TYPE_SOFTWARE:
499 if (attr->config != PERF_COUNT_SW_BPF_OUTPUT)
500 goto err_out;
501 /* fall-through */
502 case PERF_TYPE_RAW:
503 case PERF_TYPE_HARDWARE:
504 ee = bpf_event_entry_gen(perf_file, map_file);
505 if (ee)
506 return ee;
507 ee = ERR_PTR(-ENOMEM);
508 /* fall-through */
509 default:
510 break;
511 }
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700512
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200513err_out:
514 fput(perf_file);
515 return ee;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000516}
517
518static void perf_event_fd_array_put_ptr(void *ptr)
519{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200520 bpf_event_entry_free_rcu(ptr);
521}
522
523static void perf_event_fd_array_release(struct bpf_map *map,
524 struct file *map_file)
525{
526 struct bpf_array *array = container_of(map, struct bpf_array, map);
527 struct bpf_event_entry *ee;
528 int i;
529
530 rcu_read_lock();
531 for (i = 0; i < array->map.max_entries; i++) {
532 ee = READ_ONCE(array->ptrs[i]);
533 if (ee && ee->map_file == map_file)
534 fd_array_map_delete_elem(map, &i);
535 }
536 rcu_read_unlock();
Kaixu Xiaea317b22015-08-06 07:02:34 +0000537}
538
539static const struct bpf_map_ops perf_event_array_ops = {
540 .map_alloc = fd_array_map_alloc,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200541 .map_free = fd_array_map_free,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000542 .map_get_next_key = array_map_get_next_key,
543 .map_lookup_elem = fd_array_map_lookup_elem,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000544 .map_delete_elem = fd_array_map_delete_elem,
545 .map_fd_get_ptr = perf_event_fd_array_get_ptr,
546 .map_fd_put_ptr = perf_event_fd_array_put_ptr,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200547 .map_release = perf_event_fd_array_release,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000548};
549
550static struct bpf_map_type_list perf_event_array_type __read_mostly = {
551 .ops = &perf_event_array_ops,
552 .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
553};
554
555static int __init register_perf_event_array_map(void)
556{
557 bpf_register_map_type(&perf_event_array_type);
558 return 0;
559}
560late_initcall(register_perf_event_array_map);
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700561
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700562#ifdef CONFIG_CGROUPS
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700563static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
564 struct file *map_file /* not used */,
565 int fd)
566{
567 return cgroup_get_from_fd(fd);
568}
569
570static void cgroup_fd_array_put_ptr(void *ptr)
571{
572 /* cgroup_put free cgrp after a rcu grace period */
573 cgroup_put(ptr);
574}
575
576static void cgroup_fd_array_free(struct bpf_map *map)
577{
578 bpf_fd_array_map_clear(map);
579 fd_array_map_free(map);
580}
581
582static const struct bpf_map_ops cgroup_array_ops = {
583 .map_alloc = fd_array_map_alloc,
584 .map_free = cgroup_fd_array_free,
585 .map_get_next_key = array_map_get_next_key,
586 .map_lookup_elem = fd_array_map_lookup_elem,
587 .map_delete_elem = fd_array_map_delete_elem,
588 .map_fd_get_ptr = cgroup_fd_array_get_ptr,
589 .map_fd_put_ptr = cgroup_fd_array_put_ptr,
590};
591
592static struct bpf_map_type_list cgroup_array_type __read_mostly = {
593 .ops = &cgroup_array_ops,
594 .type = BPF_MAP_TYPE_CGROUP_ARRAY,
595};
596
597static int __init register_cgroup_array_map(void)
598{
599 bpf_register_map_type(&cgroup_array_type);
600 return 0;
601}
602late_initcall(register_cgroup_array_map);
603#endif