blob: 3be357ab65961a80209b5dd45acd6306900fb79c [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;
Daniel Borkmann816cfeb2018-03-08 16:17:32 +0100110 array->map.map_flags = attr->map_flags;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800111 array->elem_size = elem_size;
112
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800113 if (!percpu)
114 goto out;
115
116 array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
117
118 if (array_size >= U32_MAX - PAGE_SIZE ||
119 elem_size > PCPU_MIN_UNIT_SIZE || bpf_array_alloc_percpu(array)) {
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100120 bpf_map_area_free(array);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800121 return ERR_PTR(-ENOMEM);
122 }
123out:
124 array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
125
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800126 return &array->map;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800127}
128
129/* Called from syscall or from eBPF program */
130static void *array_map_lookup_elem(struct bpf_map *map, void *key)
131{
132 struct bpf_array *array = container_of(map, struct bpf_array, map);
133 u32 index = *(u32 *)key;
134
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800135 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800136 return NULL;
137
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800138 return array->value + array->elem_size * (index & array->index_mask);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800139}
140
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800141/* Called from eBPF program */
142static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
143{
144 struct bpf_array *array = container_of(map, struct bpf_array, map);
145 u32 index = *(u32 *)key;
146
147 if (unlikely(index >= array->map.max_entries))
148 return NULL;
149
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800150 return this_cpu_ptr(array->pptrs[index & array->index_mask]);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800151}
152
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800153int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
154{
155 struct bpf_array *array = container_of(map, struct bpf_array, map);
156 u32 index = *(u32 *)key;
157 void __percpu *pptr;
158 int cpu, off = 0;
159 u32 size;
160
161 if (unlikely(index >= array->map.max_entries))
162 return -ENOENT;
163
164 /* per_cpu areas are zero-filled and bpf programs can only
165 * access 'value_size' of them, so copying rounded areas
166 * will not leak any kernel data
167 */
168 size = round_up(map->value_size, 8);
169 rcu_read_lock();
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800170 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800171 for_each_possible_cpu(cpu) {
172 bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
173 off += size;
174 }
175 rcu_read_unlock();
176 return 0;
177}
178
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800179/* Called from syscall */
180static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
181{
182 struct bpf_array *array = container_of(map, struct bpf_array, map);
183 u32 index = *(u32 *)key;
184 u32 *next = (u32 *)next_key;
185
186 if (index >= array->map.max_entries) {
187 *next = 0;
188 return 0;
189 }
190
191 if (index == array->map.max_entries - 1)
192 return -ENOENT;
193
194 *next = index + 1;
195 return 0;
196}
197
198/* Called from syscall or from eBPF program */
199static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
200 u64 map_flags)
201{
202 struct bpf_array *array = container_of(map, struct bpf_array, map);
203 u32 index = *(u32 *)key;
204
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800205 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800206 /* unknown flags */
207 return -EINVAL;
208
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800209 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800210 /* all elements were pre-allocated, cannot insert a new one */
211 return -E2BIG;
212
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800213 if (unlikely(map_flags == BPF_NOEXIST))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800214 /* all elements already exist */
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800215 return -EEXIST;
216
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800217 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800218 memcpy(this_cpu_ptr(array->pptrs[index & array->index_mask]),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800219 value, map->value_size);
220 else
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800221 memcpy(array->value +
222 array->elem_size * (index & array->index_mask),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800223 value, map->value_size);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800224 return 0;
225}
226
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800227int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
228 u64 map_flags)
229{
230 struct bpf_array *array = container_of(map, struct bpf_array, map);
231 u32 index = *(u32 *)key;
232 void __percpu *pptr;
233 int cpu, off = 0;
234 u32 size;
235
236 if (unlikely(map_flags > BPF_EXIST))
237 /* unknown flags */
238 return -EINVAL;
239
240 if (unlikely(index >= array->map.max_entries))
241 /* all elements were pre-allocated, cannot insert a new one */
242 return -E2BIG;
243
244 if (unlikely(map_flags == BPF_NOEXIST))
245 /* all elements already exist */
246 return -EEXIST;
247
248 /* the user space will provide round_up(value_size, 8) bytes that
249 * will be copied into per-cpu area. bpf programs can only access
250 * value_size of it. During lookup the same extra bytes will be
251 * returned or zeros which were zero-filled by percpu_alloc,
252 * so no kernel data leaks possible
253 */
254 size = round_up(map->value_size, 8);
255 rcu_read_lock();
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800256 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800257 for_each_possible_cpu(cpu) {
258 bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
259 off += size;
260 }
261 rcu_read_unlock();
262 return 0;
263}
264
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800265/* Called from syscall or from eBPF program */
266static int array_map_delete_elem(struct bpf_map *map, void *key)
267{
268 return -EINVAL;
269}
270
271/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
272static void array_map_free(struct bpf_map *map)
273{
274 struct bpf_array *array = container_of(map, struct bpf_array, map);
275
276 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
277 * so the programs (can be more than one that used this map) were
278 * disconnected from events. Wait for outstanding programs to complete
279 * and free the array
280 */
281 synchronize_rcu();
282
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800283 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
284 bpf_array_free_percpu(array);
285
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100286 bpf_map_area_free(array);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800287}
288
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100289static const struct bpf_map_ops array_ops = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800290 .map_alloc = array_map_alloc,
291 .map_free = array_map_free,
292 .map_get_next_key = array_map_get_next_key,
293 .map_lookup_elem = array_map_lookup_elem,
294 .map_update_elem = array_map_update_elem,
295 .map_delete_elem = array_map_delete_elem,
296};
297
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100298static struct bpf_map_type_list array_type __read_mostly = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800299 .ops = &array_ops,
300 .type = BPF_MAP_TYPE_ARRAY,
301};
302
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800303static const struct bpf_map_ops percpu_array_ops = {
304 .map_alloc = array_map_alloc,
305 .map_free = array_map_free,
306 .map_get_next_key = array_map_get_next_key,
307 .map_lookup_elem = percpu_array_map_lookup_elem,
308 .map_update_elem = array_map_update_elem,
309 .map_delete_elem = array_map_delete_elem,
310};
311
312static struct bpf_map_type_list percpu_array_type __read_mostly = {
313 .ops = &percpu_array_ops,
314 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
315};
316
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800317static int __init register_array_map(void)
318{
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100319 bpf_register_map_type(&array_type);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800320 bpf_register_map_type(&percpu_array_type);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800321 return 0;
322}
323late_initcall(register_array_map);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700324
Wang Nan2a36f0b2015-08-06 07:02:33 +0000325static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700326{
Wang Nan2a36f0b2015-08-06 07:02:33 +0000327 /* only file descriptors can be stored in this type of map */
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700328 if (attr->value_size != sizeof(u32))
329 return ERR_PTR(-EINVAL);
330 return array_map_alloc(attr);
331}
332
Wang Nan2a36f0b2015-08-06 07:02:33 +0000333static void fd_array_map_free(struct bpf_map *map)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700334{
335 struct bpf_array *array = container_of(map, struct bpf_array, map);
336 int i;
337
338 synchronize_rcu();
339
340 /* make sure it's empty */
341 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000342 BUG_ON(array->ptrs[i] != NULL);
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100343
344 bpf_map_area_free(array);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700345}
346
Wang Nan2a36f0b2015-08-06 07:02:33 +0000347static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700348{
349 return NULL;
350}
351
352/* only called from syscall */
Daniel Borkmannd056a782016-06-15 22:47:13 +0200353int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
354 void *key, void *value, u64 map_flags)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700355{
356 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000357 void *new_ptr, *old_ptr;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700358 u32 index = *(u32 *)key, ufd;
359
360 if (map_flags != BPF_ANY)
361 return -EINVAL;
362
363 if (index >= array->map.max_entries)
364 return -E2BIG;
365
366 ufd = *(u32 *)value;
Daniel Borkmannd056a782016-06-15 22:47:13 +0200367 new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000368 if (IS_ERR(new_ptr))
369 return PTR_ERR(new_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700370
Wang Nan2a36f0b2015-08-06 07:02:33 +0000371 old_ptr = xchg(array->ptrs + index, new_ptr);
372 if (old_ptr)
373 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700374
375 return 0;
376}
377
Wang Nan2a36f0b2015-08-06 07:02:33 +0000378static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700379{
380 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000381 void *old_ptr;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700382 u32 index = *(u32 *)key;
383
384 if (index >= array->map.max_entries)
385 return -E2BIG;
386
Wang Nan2a36f0b2015-08-06 07:02:33 +0000387 old_ptr = xchg(array->ptrs + index, NULL);
388 if (old_ptr) {
389 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700390 return 0;
391 } else {
392 return -ENOENT;
393 }
394}
395
Daniel Borkmannd056a782016-06-15 22:47:13 +0200396static void *prog_fd_array_get_ptr(struct bpf_map *map,
397 struct file *map_file, int fd)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000398{
399 struct bpf_array *array = container_of(map, struct bpf_array, map);
400 struct bpf_prog *prog = bpf_prog_get(fd);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200401
Wang Nan2a36f0b2015-08-06 07:02:33 +0000402 if (IS_ERR(prog))
403 return prog;
404
405 if (!bpf_prog_array_compatible(array, prog)) {
406 bpf_prog_put(prog);
407 return ERR_PTR(-EINVAL);
408 }
Daniel Borkmannd056a782016-06-15 22:47:13 +0200409
Wang Nan2a36f0b2015-08-06 07:02:33 +0000410 return prog;
411}
412
413static void prog_fd_array_put_ptr(void *ptr)
414{
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200415 bpf_prog_put(ptr);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000416}
417
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700418/* decrement refcnt of all bpf_progs that are stored in this map */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000419void bpf_fd_array_map_clear(struct bpf_map *map)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700420{
421 struct bpf_array *array = container_of(map, struct bpf_array, map);
422 int i;
423
424 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000425 fd_array_map_delete_elem(map, &i);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700426}
427
428static const struct bpf_map_ops prog_array_ops = {
Wang Nan2a36f0b2015-08-06 07:02:33 +0000429 .map_alloc = fd_array_map_alloc,
430 .map_free = fd_array_map_free,
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700431 .map_get_next_key = array_map_get_next_key,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000432 .map_lookup_elem = fd_array_map_lookup_elem,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000433 .map_delete_elem = fd_array_map_delete_elem,
434 .map_fd_get_ptr = prog_fd_array_get_ptr,
435 .map_fd_put_ptr = prog_fd_array_put_ptr,
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700436};
437
438static struct bpf_map_type_list prog_array_type __read_mostly = {
439 .ops = &prog_array_ops,
440 .type = BPF_MAP_TYPE_PROG_ARRAY,
441};
442
443static int __init register_prog_array_map(void)
444{
445 bpf_register_map_type(&prog_array_type);
446 return 0;
447}
448late_initcall(register_prog_array_map);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000449
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200450static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
451 struct file *map_file)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000452{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200453 struct bpf_event_entry *ee;
454
Daniel Borkmann858d68f2016-07-16 01:15:55 +0200455 ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200456 if (ee) {
457 ee->event = perf_file->private_data;
458 ee->perf_file = perf_file;
459 ee->map_file = map_file;
460 }
461
462 return ee;
463}
464
465static void __bpf_event_entry_free(struct rcu_head *rcu)
466{
467 struct bpf_event_entry *ee;
468
469 ee = container_of(rcu, struct bpf_event_entry, rcu);
470 fput(ee->perf_file);
471 kfree(ee);
472}
473
474static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
475{
476 call_rcu(&ee->rcu, __bpf_event_entry_free);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000477}
478
Daniel Borkmannd056a782016-06-15 22:47:13 +0200479static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
480 struct file *map_file, int fd)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000481{
Kaixu Xiaea317b22015-08-06 07:02:34 +0000482 const struct perf_event_attr *attr;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200483 struct bpf_event_entry *ee;
484 struct perf_event *event;
485 struct file *perf_file;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000486
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200487 perf_file = perf_event_get(fd);
488 if (IS_ERR(perf_file))
489 return perf_file;
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -0800490
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200491 event = perf_file->private_data;
492 ee = ERR_PTR(-EINVAL);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000493
494 attr = perf_event_attrs(event);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200495 if (IS_ERR(attr) || attr->inherit)
496 goto err_out;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000497
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200498 switch (attr->type) {
499 case PERF_TYPE_SOFTWARE:
500 if (attr->config != PERF_COUNT_SW_BPF_OUTPUT)
501 goto err_out;
502 /* fall-through */
503 case PERF_TYPE_RAW:
504 case PERF_TYPE_HARDWARE:
505 ee = bpf_event_entry_gen(perf_file, map_file);
506 if (ee)
507 return ee;
508 ee = ERR_PTR(-ENOMEM);
509 /* fall-through */
510 default:
511 break;
512 }
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700513
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200514err_out:
515 fput(perf_file);
516 return ee;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000517}
518
519static void perf_event_fd_array_put_ptr(void *ptr)
520{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200521 bpf_event_entry_free_rcu(ptr);
522}
523
524static void perf_event_fd_array_release(struct bpf_map *map,
525 struct file *map_file)
526{
527 struct bpf_array *array = container_of(map, struct bpf_array, map);
528 struct bpf_event_entry *ee;
529 int i;
530
531 rcu_read_lock();
532 for (i = 0; i < array->map.max_entries; i++) {
533 ee = READ_ONCE(array->ptrs[i]);
534 if (ee && ee->map_file == map_file)
535 fd_array_map_delete_elem(map, &i);
536 }
537 rcu_read_unlock();
Kaixu Xiaea317b22015-08-06 07:02:34 +0000538}
539
540static const struct bpf_map_ops perf_event_array_ops = {
541 .map_alloc = fd_array_map_alloc,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200542 .map_free = fd_array_map_free,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000543 .map_get_next_key = array_map_get_next_key,
544 .map_lookup_elem = fd_array_map_lookup_elem,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000545 .map_delete_elem = fd_array_map_delete_elem,
546 .map_fd_get_ptr = perf_event_fd_array_get_ptr,
547 .map_fd_put_ptr = perf_event_fd_array_put_ptr,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200548 .map_release = perf_event_fd_array_release,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000549};
550
551static struct bpf_map_type_list perf_event_array_type __read_mostly = {
552 .ops = &perf_event_array_ops,
553 .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
554};
555
556static int __init register_perf_event_array_map(void)
557{
558 bpf_register_map_type(&perf_event_array_type);
559 return 0;
560}
561late_initcall(register_perf_event_array_map);
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700562
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700563#ifdef CONFIG_CGROUPS
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700564static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
565 struct file *map_file /* not used */,
566 int fd)
567{
568 return cgroup_get_from_fd(fd);
569}
570
571static void cgroup_fd_array_put_ptr(void *ptr)
572{
573 /* cgroup_put free cgrp after a rcu grace period */
574 cgroup_put(ptr);
575}
576
577static void cgroup_fd_array_free(struct bpf_map *map)
578{
579 bpf_fd_array_map_clear(map);
580 fd_array_map_free(map);
581}
582
583static const struct bpf_map_ops cgroup_array_ops = {
584 .map_alloc = fd_array_map_alloc,
585 .map_free = cgroup_fd_array_free,
586 .map_get_next_key = array_map_get_next_key,
587 .map_lookup_elem = fd_array_map_lookup_elem,
588 .map_delete_elem = fd_array_map_delete_elem,
589 .map_fd_get_ptr = cgroup_fd_array_get_ptr,
590 .map_fd_put_ptr = cgroup_fd_array_put_ptr,
591};
592
593static struct bpf_map_type_list cgroup_array_type __read_mostly = {
594 .ops = &cgroup_array_ops,
595 .type = BPF_MAP_TYPE_CGROUP_ARRAY,
596};
597
598static int __init register_cgroup_array_map(void)
599{
600 bpf_register_map_type(&cgroup_array_type);
601 return 0;
602}
603late_initcall(register_cgroup_array_map);
604#endif