blob: 4d7d5d0ed76a6dd61b36c412c49d46dcf872b5c2 [file] [log] [blame]
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -08001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07002 * Copyright (c) 2016,2017 Facebook
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -08003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/bpf.h>
14#include <linux/err.h>
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080015#include <linux/slab.h>
16#include <linux/mm.h>
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -070017#include <linux/filter.h>
Daniel Borkmann0cdf56402015-10-02 18:42:00 +020018#include <linux/perf_event.h>
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080019
Alexei Starovoitova10423b2016-02-01 22:39:54 -080020static void bpf_array_free_percpu(struct bpf_array *array)
21{
22 int i;
23
24 for (i = 0; i < array->map.max_entries; i++)
25 free_percpu(array->pptrs[i]);
26}
27
28static int bpf_array_alloc_percpu(struct bpf_array *array)
29{
30 void __percpu *ptr;
31 int i;
32
33 for (i = 0; i < array->map.max_entries; i++) {
34 ptr = __alloc_percpu_gfp(array->elem_size, 8,
35 GFP_USER | __GFP_NOWARN);
36 if (!ptr) {
37 bpf_array_free_percpu(array);
38 return -ENOMEM;
39 }
40 array->pptrs[i] = ptr;
41 }
42
43 return 0;
44}
45
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080046/* Called from syscall */
47static struct bpf_map *array_map_alloc(union bpf_attr *attr)
48{
Alexei Starovoitova10423b2016-02-01 22:39:54 -080049 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080050 struct bpf_array *array;
Alexei Starovoitova10423b2016-02-01 22:39:54 -080051 u64 array_size;
52 u32 elem_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
Michal Hocko7984c272017-01-10 16:57:30 -080059 if (attr->value_size > KMALLOC_MAX_SIZE)
Alexei Starovoitov01b3f522015-11-29 16:59:35 -080060 /* 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 Starovoitova10423b2016-02-01 22:39:54 -080067 array_size = sizeof(*array);
68 if (percpu)
69 array_size += (u64) attr->max_entries * sizeof(void *);
70 else
71 array_size += (u64) attr->max_entries * elem_size;
72
73 /* make sure there is no u32 overflow later in round_up() */
74 if (array_size >= U32_MAX - PAGE_SIZE)
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -080075 return ERR_PTR(-ENOMEM);
76
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080077 /* allocate all map elements and zero-initialize them */
Daniel Borkmannd407bd22017-01-18 15:14:17 +010078 array = bpf_map_area_alloc(array_size);
79 if (!array)
80 return ERR_PTR(-ENOMEM);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080081
82 /* copy mandatory map attributes */
Alexei Starovoitova10423b2016-02-01 22:39:54 -080083 array->map.map_type = attr->map_type;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080084 array->map.key_size = attr->key_size;
85 array->map.value_size = attr->value_size;
86 array->map.max_entries = attr->max_entries;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080087 array->elem_size = elem_size;
88
Alexei Starovoitova10423b2016-02-01 22:39:54 -080089 if (!percpu)
90 goto out;
91
92 array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
93
94 if (array_size >= U32_MAX - PAGE_SIZE ||
95 elem_size > PCPU_MIN_UNIT_SIZE || bpf_array_alloc_percpu(array)) {
Daniel Borkmannd407bd22017-01-18 15:14:17 +010096 bpf_map_area_free(array);
Alexei Starovoitova10423b2016-02-01 22:39:54 -080097 return ERR_PTR(-ENOMEM);
98 }
99out:
100 array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
101
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800102 return &array->map;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800103}
104
105/* Called from syscall or from eBPF program */
106static void *array_map_lookup_elem(struct bpf_map *map, void *key)
107{
108 struct bpf_array *array = container_of(map, struct bpf_array, map);
109 u32 index = *(u32 *)key;
110
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800111 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800112 return NULL;
113
114 return array->value + array->elem_size * index;
115}
116
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700117/* emit BPF instructions equivalent to C code of array_map_lookup_elem() */
118static u32 array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
119{
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700120 struct bpf_insn *insn = insn_buf;
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700121 u32 elem_size = round_up(map->value_size, 8);
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700122 const int ret = BPF_REG_0;
123 const int map_ptr = BPF_REG_1;
124 const int index = BPF_REG_2;
125
126 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
127 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700128 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3);
129
130 if (is_power_of_2(elem_size)) {
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700131 *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
132 } else {
133 *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
134 }
135 *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
136 *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
137 *insn++ = BPF_MOV64_IMM(ret, 0);
138 return insn - insn_buf;
139}
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
150 return this_cpu_ptr(array->pptrs[index]);
151}
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();
170 pptr = array->pptrs[index];
171 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)
218 memcpy(this_cpu_ptr(array->pptrs[index]),
219 value, map->value_size);
220 else
221 memcpy(array->value + array->elem_size * index,
222 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();
255 pptr = array->pptrs[index];
256 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 Borkmannd407bd22017-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,
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700295 .map_gen_lookup = array_map_gen_lookup,
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800296};
297
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +0100298static struct bpf_map_type_list array_type __ro_after_init = {
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
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +0100312static struct bpf_map_type_list percpu_array_type __ro_after_init = {
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800313 .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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Borkmannd407bd22017-01-18 15:14:17 +0100343
344 bpf_map_area_free(array);
Alexei Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-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 Starovoitov04fd61ab2015-05-19 16:59:03 -0700436};
437
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +0100438static struct bpf_map_type_list prog_array_type __ro_after_init = {
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700439 .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
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +0100551static struct bpf_map_type_list perf_event_array_type __ro_after_init = {
Kaixu Xiaea317b22015-08-06 07:02:34 +0000552 .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
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +0100593static struct bpf_map_type_list cgroup_array_type __ro_after_init = {
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700594 .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