blob: bc57ead11b0fbd4828b1681df785b5b767bba307 [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;
Alexei Starovoitova10423b2016-02-01 22:39:54 -080052 u64 array_size;
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;
68 index_mask = roundup_pow_of_two(max_entries) - 1;
69
70 if (unpriv)
71 /* round up array size to nearest power of 2,
72 * since cpu will speculate within index_mask limits
73 */
74 max_entries = index_mask + 1;
75
Alexei Starovoitova10423b2016-02-01 22:39:54 -080076 array_size = sizeof(*array);
77 if (percpu)
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080078 array_size += (u64) max_entries * sizeof(void *);
Alexei Starovoitova10423b2016-02-01 22:39:54 -080079 else
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080080 array_size += (u64) max_entries * elem_size;
Alexei Starovoitova10423b2016-02-01 22:39:54 -080081
82 /* make sure there is no u32 overflow later in round_up() */
83 if (array_size >= U32_MAX - PAGE_SIZE)
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -080084 return ERR_PTR(-ENOMEM);
85
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080086 /* allocate all map elements and zero-initialize them */
Daniel Borkmann251d00b2017-01-18 15:14:17 +010087 array = bpf_map_area_alloc(array_size);
88 if (!array)
89 return ERR_PTR(-ENOMEM);
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080090 array->index_mask = index_mask;
91 array->map.unpriv_array = unpriv;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080092
93 /* copy mandatory map attributes */
Alexei Starovoitova10423b2016-02-01 22:39:54 -080094 array->map.map_type = attr->map_type;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080095 array->map.key_size = attr->key_size;
96 array->map.value_size = attr->value_size;
97 array->map.max_entries = attr->max_entries;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080098 array->elem_size = elem_size;
99
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800100 if (!percpu)
101 goto out;
102
103 array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
104
105 if (array_size >= U32_MAX - PAGE_SIZE ||
106 elem_size > PCPU_MIN_UNIT_SIZE || bpf_array_alloc_percpu(array)) {
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100107 bpf_map_area_free(array);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800108 return ERR_PTR(-ENOMEM);
109 }
110out:
111 array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
112
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800113 return &array->map;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800114}
115
116/* Called from syscall or from eBPF program */
117static void *array_map_lookup_elem(struct bpf_map *map, void *key)
118{
119 struct bpf_array *array = container_of(map, struct bpf_array, map);
120 u32 index = *(u32 *)key;
121
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800122 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800123 return NULL;
124
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800125 return array->value + array->elem_size * (index & array->index_mask);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800126}
127
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800128/* Called from eBPF program */
129static void *percpu_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
134 if (unlikely(index >= array->map.max_entries))
135 return NULL;
136
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800137 return this_cpu_ptr(array->pptrs[index & array->index_mask]);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800138}
139
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800140int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
141{
142 struct bpf_array *array = container_of(map, struct bpf_array, map);
143 u32 index = *(u32 *)key;
144 void __percpu *pptr;
145 int cpu, off = 0;
146 u32 size;
147
148 if (unlikely(index >= array->map.max_entries))
149 return -ENOENT;
150
151 /* per_cpu areas are zero-filled and bpf programs can only
152 * access 'value_size' of them, so copying rounded areas
153 * will not leak any kernel data
154 */
155 size = round_up(map->value_size, 8);
156 rcu_read_lock();
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800157 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800158 for_each_possible_cpu(cpu) {
159 bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
160 off += size;
161 }
162 rcu_read_unlock();
163 return 0;
164}
165
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800166/* Called from syscall */
167static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
168{
169 struct bpf_array *array = container_of(map, struct bpf_array, map);
170 u32 index = *(u32 *)key;
171 u32 *next = (u32 *)next_key;
172
173 if (index >= array->map.max_entries) {
174 *next = 0;
175 return 0;
176 }
177
178 if (index == array->map.max_entries - 1)
179 return -ENOENT;
180
181 *next = index + 1;
182 return 0;
183}
184
185/* Called from syscall or from eBPF program */
186static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
187 u64 map_flags)
188{
189 struct bpf_array *array = container_of(map, struct bpf_array, map);
190 u32 index = *(u32 *)key;
191
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800192 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800193 /* unknown flags */
194 return -EINVAL;
195
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800196 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800197 /* all elements were pre-allocated, cannot insert a new one */
198 return -E2BIG;
199
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800200 if (unlikely(map_flags == BPF_NOEXIST))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800201 /* all elements already exist */
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800202 return -EEXIST;
203
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800204 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800205 memcpy(this_cpu_ptr(array->pptrs[index & array->index_mask]),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800206 value, map->value_size);
207 else
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800208 memcpy(array->value +
209 array->elem_size * (index & array->index_mask),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800210 value, map->value_size);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800211 return 0;
212}
213
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800214int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
215 u64 map_flags)
216{
217 struct bpf_array *array = container_of(map, struct bpf_array, map);
218 u32 index = *(u32 *)key;
219 void __percpu *pptr;
220 int cpu, off = 0;
221 u32 size;
222
223 if (unlikely(map_flags > BPF_EXIST))
224 /* unknown flags */
225 return -EINVAL;
226
227 if (unlikely(index >= array->map.max_entries))
228 /* all elements were pre-allocated, cannot insert a new one */
229 return -E2BIG;
230
231 if (unlikely(map_flags == BPF_NOEXIST))
232 /* all elements already exist */
233 return -EEXIST;
234
235 /* the user space will provide round_up(value_size, 8) bytes that
236 * will be copied into per-cpu area. bpf programs can only access
237 * value_size of it. During lookup the same extra bytes will be
238 * returned or zeros which were zero-filled by percpu_alloc,
239 * so no kernel data leaks possible
240 */
241 size = round_up(map->value_size, 8);
242 rcu_read_lock();
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800243 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800244 for_each_possible_cpu(cpu) {
245 bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
246 off += size;
247 }
248 rcu_read_unlock();
249 return 0;
250}
251
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800252/* Called from syscall or from eBPF program */
253static int array_map_delete_elem(struct bpf_map *map, void *key)
254{
255 return -EINVAL;
256}
257
258/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
259static void array_map_free(struct bpf_map *map)
260{
261 struct bpf_array *array = container_of(map, struct bpf_array, map);
262
263 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
264 * so the programs (can be more than one that used this map) were
265 * disconnected from events. Wait for outstanding programs to complete
266 * and free the array
267 */
268 synchronize_rcu();
269
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800270 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
271 bpf_array_free_percpu(array);
272
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100273 bpf_map_area_free(array);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800274}
275
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100276static const struct bpf_map_ops array_ops = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800277 .map_alloc = array_map_alloc,
278 .map_free = array_map_free,
279 .map_get_next_key = array_map_get_next_key,
280 .map_lookup_elem = array_map_lookup_elem,
281 .map_update_elem = array_map_update_elem,
282 .map_delete_elem = array_map_delete_elem,
283};
284
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100285static struct bpf_map_type_list array_type __read_mostly = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800286 .ops = &array_ops,
287 .type = BPF_MAP_TYPE_ARRAY,
288};
289
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800290static const struct bpf_map_ops percpu_array_ops = {
291 .map_alloc = array_map_alloc,
292 .map_free = array_map_free,
293 .map_get_next_key = array_map_get_next_key,
294 .map_lookup_elem = percpu_array_map_lookup_elem,
295 .map_update_elem = array_map_update_elem,
296 .map_delete_elem = array_map_delete_elem,
297};
298
299static struct bpf_map_type_list percpu_array_type __read_mostly = {
300 .ops = &percpu_array_ops,
301 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
302};
303
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800304static int __init register_array_map(void)
305{
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100306 bpf_register_map_type(&array_type);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800307 bpf_register_map_type(&percpu_array_type);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800308 return 0;
309}
310late_initcall(register_array_map);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700311
Wang Nan2a36f0b2015-08-06 07:02:33 +0000312static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700313{
Wang Nan2a36f0b2015-08-06 07:02:33 +0000314 /* only file descriptors can be stored in this type of map */
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700315 if (attr->value_size != sizeof(u32))
316 return ERR_PTR(-EINVAL);
317 return array_map_alloc(attr);
318}
319
Wang Nan2a36f0b2015-08-06 07:02:33 +0000320static void fd_array_map_free(struct bpf_map *map)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700321{
322 struct bpf_array *array = container_of(map, struct bpf_array, map);
323 int i;
324
325 synchronize_rcu();
326
327 /* make sure it's empty */
328 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000329 BUG_ON(array->ptrs[i] != NULL);
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100330
331 bpf_map_area_free(array);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700332}
333
Wang Nan2a36f0b2015-08-06 07:02:33 +0000334static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700335{
336 return NULL;
337}
338
339/* only called from syscall */
Daniel Borkmannd056a782016-06-15 22:47:13 +0200340int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
341 void *key, void *value, u64 map_flags)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700342{
343 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000344 void *new_ptr, *old_ptr;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700345 u32 index = *(u32 *)key, ufd;
346
347 if (map_flags != BPF_ANY)
348 return -EINVAL;
349
350 if (index >= array->map.max_entries)
351 return -E2BIG;
352
353 ufd = *(u32 *)value;
Daniel Borkmannd056a782016-06-15 22:47:13 +0200354 new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000355 if (IS_ERR(new_ptr))
356 return PTR_ERR(new_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700357
Wang Nan2a36f0b2015-08-06 07:02:33 +0000358 old_ptr = xchg(array->ptrs + index, new_ptr);
359 if (old_ptr)
360 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700361
362 return 0;
363}
364
Wang Nan2a36f0b2015-08-06 07:02:33 +0000365static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700366{
367 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000368 void *old_ptr;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700369 u32 index = *(u32 *)key;
370
371 if (index >= array->map.max_entries)
372 return -E2BIG;
373
Wang Nan2a36f0b2015-08-06 07:02:33 +0000374 old_ptr = xchg(array->ptrs + index, NULL);
375 if (old_ptr) {
376 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700377 return 0;
378 } else {
379 return -ENOENT;
380 }
381}
382
Daniel Borkmannd056a782016-06-15 22:47:13 +0200383static void *prog_fd_array_get_ptr(struct bpf_map *map,
384 struct file *map_file, int fd)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000385{
386 struct bpf_array *array = container_of(map, struct bpf_array, map);
387 struct bpf_prog *prog = bpf_prog_get(fd);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200388
Wang Nan2a36f0b2015-08-06 07:02:33 +0000389 if (IS_ERR(prog))
390 return prog;
391
392 if (!bpf_prog_array_compatible(array, prog)) {
393 bpf_prog_put(prog);
394 return ERR_PTR(-EINVAL);
395 }
Daniel Borkmannd056a782016-06-15 22:47:13 +0200396
Wang Nan2a36f0b2015-08-06 07:02:33 +0000397 return prog;
398}
399
400static void prog_fd_array_put_ptr(void *ptr)
401{
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200402 bpf_prog_put(ptr);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000403}
404
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700405/* decrement refcnt of all bpf_progs that are stored in this map */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000406void bpf_fd_array_map_clear(struct bpf_map *map)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700407{
408 struct bpf_array *array = container_of(map, struct bpf_array, map);
409 int i;
410
411 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000412 fd_array_map_delete_elem(map, &i);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700413}
414
415static const struct bpf_map_ops prog_array_ops = {
Wang Nan2a36f0b2015-08-06 07:02:33 +0000416 .map_alloc = fd_array_map_alloc,
417 .map_free = fd_array_map_free,
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700418 .map_get_next_key = array_map_get_next_key,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000419 .map_lookup_elem = fd_array_map_lookup_elem,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000420 .map_delete_elem = fd_array_map_delete_elem,
421 .map_fd_get_ptr = prog_fd_array_get_ptr,
422 .map_fd_put_ptr = prog_fd_array_put_ptr,
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700423};
424
425static struct bpf_map_type_list prog_array_type __read_mostly = {
426 .ops = &prog_array_ops,
427 .type = BPF_MAP_TYPE_PROG_ARRAY,
428};
429
430static int __init register_prog_array_map(void)
431{
432 bpf_register_map_type(&prog_array_type);
433 return 0;
434}
435late_initcall(register_prog_array_map);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000436
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200437static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
438 struct file *map_file)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000439{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200440 struct bpf_event_entry *ee;
441
Daniel Borkmann858d68f2016-07-16 01:15:55 +0200442 ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200443 if (ee) {
444 ee->event = perf_file->private_data;
445 ee->perf_file = perf_file;
446 ee->map_file = map_file;
447 }
448
449 return ee;
450}
451
452static void __bpf_event_entry_free(struct rcu_head *rcu)
453{
454 struct bpf_event_entry *ee;
455
456 ee = container_of(rcu, struct bpf_event_entry, rcu);
457 fput(ee->perf_file);
458 kfree(ee);
459}
460
461static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
462{
463 call_rcu(&ee->rcu, __bpf_event_entry_free);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000464}
465
Daniel Borkmannd056a782016-06-15 22:47:13 +0200466static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
467 struct file *map_file, int fd)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000468{
Kaixu Xiaea317b22015-08-06 07:02:34 +0000469 const struct perf_event_attr *attr;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200470 struct bpf_event_entry *ee;
471 struct perf_event *event;
472 struct file *perf_file;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000473
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200474 perf_file = perf_event_get(fd);
475 if (IS_ERR(perf_file))
476 return perf_file;
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -0800477
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200478 event = perf_file->private_data;
479 ee = ERR_PTR(-EINVAL);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000480
481 attr = perf_event_attrs(event);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200482 if (IS_ERR(attr) || attr->inherit)
483 goto err_out;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000484
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200485 switch (attr->type) {
486 case PERF_TYPE_SOFTWARE:
487 if (attr->config != PERF_COUNT_SW_BPF_OUTPUT)
488 goto err_out;
489 /* fall-through */
490 case PERF_TYPE_RAW:
491 case PERF_TYPE_HARDWARE:
492 ee = bpf_event_entry_gen(perf_file, map_file);
493 if (ee)
494 return ee;
495 ee = ERR_PTR(-ENOMEM);
496 /* fall-through */
497 default:
498 break;
499 }
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700500
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200501err_out:
502 fput(perf_file);
503 return ee;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000504}
505
506static void perf_event_fd_array_put_ptr(void *ptr)
507{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200508 bpf_event_entry_free_rcu(ptr);
509}
510
511static void perf_event_fd_array_release(struct bpf_map *map,
512 struct file *map_file)
513{
514 struct bpf_array *array = container_of(map, struct bpf_array, map);
515 struct bpf_event_entry *ee;
516 int i;
517
518 rcu_read_lock();
519 for (i = 0; i < array->map.max_entries; i++) {
520 ee = READ_ONCE(array->ptrs[i]);
521 if (ee && ee->map_file == map_file)
522 fd_array_map_delete_elem(map, &i);
523 }
524 rcu_read_unlock();
Kaixu Xiaea317b22015-08-06 07:02:34 +0000525}
526
527static const struct bpf_map_ops perf_event_array_ops = {
528 .map_alloc = fd_array_map_alloc,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200529 .map_free = fd_array_map_free,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000530 .map_get_next_key = array_map_get_next_key,
531 .map_lookup_elem = fd_array_map_lookup_elem,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000532 .map_delete_elem = fd_array_map_delete_elem,
533 .map_fd_get_ptr = perf_event_fd_array_get_ptr,
534 .map_fd_put_ptr = perf_event_fd_array_put_ptr,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200535 .map_release = perf_event_fd_array_release,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000536};
537
538static struct bpf_map_type_list perf_event_array_type __read_mostly = {
539 .ops = &perf_event_array_ops,
540 .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
541};
542
543static int __init register_perf_event_array_map(void)
544{
545 bpf_register_map_type(&perf_event_array_type);
546 return 0;
547}
548late_initcall(register_perf_event_array_map);
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700549
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700550#ifdef CONFIG_CGROUPS
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700551static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
552 struct file *map_file /* not used */,
553 int fd)
554{
555 return cgroup_get_from_fd(fd);
556}
557
558static void cgroup_fd_array_put_ptr(void *ptr)
559{
560 /* cgroup_put free cgrp after a rcu grace period */
561 cgroup_put(ptr);
562}
563
564static void cgroup_fd_array_free(struct bpf_map *map)
565{
566 bpf_fd_array_map_clear(map);
567 fd_array_map_free(map);
568}
569
570static const struct bpf_map_ops cgroup_array_ops = {
571 .map_alloc = fd_array_map_alloc,
572 .map_free = cgroup_fd_array_free,
573 .map_get_next_key = array_map_get_next_key,
574 .map_lookup_elem = fd_array_map_lookup_elem,
575 .map_delete_elem = fd_array_map_delete_elem,
576 .map_fd_get_ptr = cgroup_fd_array_get_ptr,
577 .map_fd_put_ptr = cgroup_fd_array_put_ptr,
578};
579
580static struct bpf_map_type_list cgroup_array_type __read_mostly = {
581 .ops = &cgroup_array_ops,
582 .type = BPF_MAP_TYPE_CGROUP_ARRAY,
583};
584
585static int __init register_cgroup_array_map(void)
586{
587 bpf_register_map_type(&cgroup_array_type);
588 return 0;
589}
590late_initcall(register_cgroup_array_map);
591#endif