blob: bcf9955fac956cc7c92837089aad6526c90aadc6 [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{
120 struct bpf_array *array = container_of(map, struct bpf_array, map);
121 struct bpf_insn *insn = insn_buf;
122 u32 elem_size = array->elem_size;
123 const int ret = BPF_REG_0;
124 const int map_ptr = BPF_REG_1;
125 const int index = BPF_REG_2;
126
127 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
128 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
129 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, array->map.max_entries,
130 elem_size == 1 ? 2 : 3);
131 if (elem_size == 1) {
132 /* nop */
133 } else if (is_power_of_2(elem_size)) {
134 *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
135 } else {
136 *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
137 }
138 *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
139 *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
140 *insn++ = BPF_MOV64_IMM(ret, 0);
141 return insn - insn_buf;
142}
143
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800144/* Called from eBPF program */
145static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
146{
147 struct bpf_array *array = container_of(map, struct bpf_array, map);
148 u32 index = *(u32 *)key;
149
150 if (unlikely(index >= array->map.max_entries))
151 return NULL;
152
153 return this_cpu_ptr(array->pptrs[index]);
154}
155
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800156int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
157{
158 struct bpf_array *array = container_of(map, struct bpf_array, map);
159 u32 index = *(u32 *)key;
160 void __percpu *pptr;
161 int cpu, off = 0;
162 u32 size;
163
164 if (unlikely(index >= array->map.max_entries))
165 return -ENOENT;
166
167 /* per_cpu areas are zero-filled and bpf programs can only
168 * access 'value_size' of them, so copying rounded areas
169 * will not leak any kernel data
170 */
171 size = round_up(map->value_size, 8);
172 rcu_read_lock();
173 pptr = array->pptrs[index];
174 for_each_possible_cpu(cpu) {
175 bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
176 off += size;
177 }
178 rcu_read_unlock();
179 return 0;
180}
181
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800182/* Called from syscall */
183static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
184{
185 struct bpf_array *array = container_of(map, struct bpf_array, map);
186 u32 index = *(u32 *)key;
187 u32 *next = (u32 *)next_key;
188
189 if (index >= array->map.max_entries) {
190 *next = 0;
191 return 0;
192 }
193
194 if (index == array->map.max_entries - 1)
195 return -ENOENT;
196
197 *next = index + 1;
198 return 0;
199}
200
201/* Called from syscall or from eBPF program */
202static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
203 u64 map_flags)
204{
205 struct bpf_array *array = container_of(map, struct bpf_array, map);
206 u32 index = *(u32 *)key;
207
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800208 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800209 /* unknown flags */
210 return -EINVAL;
211
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800212 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800213 /* all elements were pre-allocated, cannot insert a new one */
214 return -E2BIG;
215
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800216 if (unlikely(map_flags == BPF_NOEXIST))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800217 /* all elements already exist */
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800218 return -EEXIST;
219
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800220 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
221 memcpy(this_cpu_ptr(array->pptrs[index]),
222 value, map->value_size);
223 else
224 memcpy(array->value + array->elem_size * index,
225 value, map->value_size);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800226 return 0;
227}
228
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800229int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
230 u64 map_flags)
231{
232 struct bpf_array *array = container_of(map, struct bpf_array, map);
233 u32 index = *(u32 *)key;
234 void __percpu *pptr;
235 int cpu, off = 0;
236 u32 size;
237
238 if (unlikely(map_flags > BPF_EXIST))
239 /* unknown flags */
240 return -EINVAL;
241
242 if (unlikely(index >= array->map.max_entries))
243 /* all elements were pre-allocated, cannot insert a new one */
244 return -E2BIG;
245
246 if (unlikely(map_flags == BPF_NOEXIST))
247 /* all elements already exist */
248 return -EEXIST;
249
250 /* the user space will provide round_up(value_size, 8) bytes that
251 * will be copied into per-cpu area. bpf programs can only access
252 * value_size of it. During lookup the same extra bytes will be
253 * returned or zeros which were zero-filled by percpu_alloc,
254 * so no kernel data leaks possible
255 */
256 size = round_up(map->value_size, 8);
257 rcu_read_lock();
258 pptr = array->pptrs[index];
259 for_each_possible_cpu(cpu) {
260 bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
261 off += size;
262 }
263 rcu_read_unlock();
264 return 0;
265}
266
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800267/* Called from syscall or from eBPF program */
268static int array_map_delete_elem(struct bpf_map *map, void *key)
269{
270 return -EINVAL;
271}
272
273/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
274static void array_map_free(struct bpf_map *map)
275{
276 struct bpf_array *array = container_of(map, struct bpf_array, map);
277
278 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
279 * so the programs (can be more than one that used this map) were
280 * disconnected from events. Wait for outstanding programs to complete
281 * and free the array
282 */
283 synchronize_rcu();
284
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800285 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
286 bpf_array_free_percpu(array);
287
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100288 bpf_map_area_free(array);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800289}
290
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100291static const struct bpf_map_ops array_ops = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800292 .map_alloc = array_map_alloc,
293 .map_free = array_map_free,
294 .map_get_next_key = array_map_get_next_key,
295 .map_lookup_elem = array_map_lookup_elem,
296 .map_update_elem = array_map_update_elem,
297 .map_delete_elem = array_map_delete_elem,
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700298 .map_gen_lookup = array_map_gen_lookup,
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800299};
300
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +0100301static struct bpf_map_type_list array_type __ro_after_init = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800302 .ops = &array_ops,
303 .type = BPF_MAP_TYPE_ARRAY,
304};
305
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800306static const struct bpf_map_ops percpu_array_ops = {
307 .map_alloc = array_map_alloc,
308 .map_free = array_map_free,
309 .map_get_next_key = array_map_get_next_key,
310 .map_lookup_elem = percpu_array_map_lookup_elem,
311 .map_update_elem = array_map_update_elem,
312 .map_delete_elem = array_map_delete_elem,
313};
314
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +0100315static struct bpf_map_type_list percpu_array_type __ro_after_init = {
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800316 .ops = &percpu_array_ops,
317 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
318};
319
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800320static int __init register_array_map(void)
321{
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100322 bpf_register_map_type(&array_type);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800323 bpf_register_map_type(&percpu_array_type);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800324 return 0;
325}
326late_initcall(register_array_map);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700327
Wang Nan2a36f0b2015-08-06 07:02:33 +0000328static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700329{
Wang Nan2a36f0b2015-08-06 07:02:33 +0000330 /* only file descriptors can be stored in this type of map */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700331 if (attr->value_size != sizeof(u32))
332 return ERR_PTR(-EINVAL);
333 return array_map_alloc(attr);
334}
335
Wang Nan2a36f0b2015-08-06 07:02:33 +0000336static void fd_array_map_free(struct bpf_map *map)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700337{
338 struct bpf_array *array = container_of(map, struct bpf_array, map);
339 int i;
340
341 synchronize_rcu();
342
343 /* make sure it's empty */
344 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000345 BUG_ON(array->ptrs[i] != NULL);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100346
347 bpf_map_area_free(array);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700348}
349
Wang Nan2a36f0b2015-08-06 07:02:33 +0000350static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700351{
352 return NULL;
353}
354
355/* only called from syscall */
Daniel Borkmannd056a782016-06-15 22:47:13 +0200356int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
357 void *key, void *value, u64 map_flags)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700358{
359 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000360 void *new_ptr, *old_ptr;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700361 u32 index = *(u32 *)key, ufd;
362
363 if (map_flags != BPF_ANY)
364 return -EINVAL;
365
366 if (index >= array->map.max_entries)
367 return -E2BIG;
368
369 ufd = *(u32 *)value;
Daniel Borkmannd056a782016-06-15 22:47:13 +0200370 new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000371 if (IS_ERR(new_ptr))
372 return PTR_ERR(new_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700373
Wang Nan2a36f0b2015-08-06 07:02:33 +0000374 old_ptr = xchg(array->ptrs + index, new_ptr);
375 if (old_ptr)
376 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700377
378 return 0;
379}
380
Wang Nan2a36f0b2015-08-06 07:02:33 +0000381static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700382{
383 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000384 void *old_ptr;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700385 u32 index = *(u32 *)key;
386
387 if (index >= array->map.max_entries)
388 return -E2BIG;
389
Wang Nan2a36f0b2015-08-06 07:02:33 +0000390 old_ptr = xchg(array->ptrs + index, NULL);
391 if (old_ptr) {
392 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700393 return 0;
394 } else {
395 return -ENOENT;
396 }
397}
398
Daniel Borkmannd056a782016-06-15 22:47:13 +0200399static void *prog_fd_array_get_ptr(struct bpf_map *map,
400 struct file *map_file, int fd)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000401{
402 struct bpf_array *array = container_of(map, struct bpf_array, map);
403 struct bpf_prog *prog = bpf_prog_get(fd);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200404
Wang Nan2a36f0b2015-08-06 07:02:33 +0000405 if (IS_ERR(prog))
406 return prog;
407
408 if (!bpf_prog_array_compatible(array, prog)) {
409 bpf_prog_put(prog);
410 return ERR_PTR(-EINVAL);
411 }
Daniel Borkmannd056a782016-06-15 22:47:13 +0200412
Wang Nan2a36f0b2015-08-06 07:02:33 +0000413 return prog;
414}
415
416static void prog_fd_array_put_ptr(void *ptr)
417{
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200418 bpf_prog_put(ptr);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000419}
420
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700421/* decrement refcnt of all bpf_progs that are stored in this map */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000422void bpf_fd_array_map_clear(struct bpf_map *map)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700423{
424 struct bpf_array *array = container_of(map, struct bpf_array, map);
425 int i;
426
427 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000428 fd_array_map_delete_elem(map, &i);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700429}
430
431static const struct bpf_map_ops prog_array_ops = {
Wang Nan2a36f0b2015-08-06 07:02:33 +0000432 .map_alloc = fd_array_map_alloc,
433 .map_free = fd_array_map_free,
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700434 .map_get_next_key = array_map_get_next_key,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000435 .map_lookup_elem = fd_array_map_lookup_elem,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000436 .map_delete_elem = fd_array_map_delete_elem,
437 .map_fd_get_ptr = prog_fd_array_get_ptr,
438 .map_fd_put_ptr = prog_fd_array_put_ptr,
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700439};
440
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +0100441static struct bpf_map_type_list prog_array_type __ro_after_init = {
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700442 .ops = &prog_array_ops,
443 .type = BPF_MAP_TYPE_PROG_ARRAY,
444};
445
446static int __init register_prog_array_map(void)
447{
448 bpf_register_map_type(&prog_array_type);
449 return 0;
450}
451late_initcall(register_prog_array_map);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000452
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200453static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
454 struct file *map_file)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000455{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200456 struct bpf_event_entry *ee;
457
Daniel Borkmann858d68f2016-07-16 01:15:55 +0200458 ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200459 if (ee) {
460 ee->event = perf_file->private_data;
461 ee->perf_file = perf_file;
462 ee->map_file = map_file;
463 }
464
465 return ee;
466}
467
468static void __bpf_event_entry_free(struct rcu_head *rcu)
469{
470 struct bpf_event_entry *ee;
471
472 ee = container_of(rcu, struct bpf_event_entry, rcu);
473 fput(ee->perf_file);
474 kfree(ee);
475}
476
477static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
478{
479 call_rcu(&ee->rcu, __bpf_event_entry_free);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000480}
481
Daniel Borkmannd056a782016-06-15 22:47:13 +0200482static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
483 struct file *map_file, int fd)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000484{
Kaixu Xiaea317b22015-08-06 07:02:34 +0000485 const struct perf_event_attr *attr;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200486 struct bpf_event_entry *ee;
487 struct perf_event *event;
488 struct file *perf_file;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000489
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200490 perf_file = perf_event_get(fd);
491 if (IS_ERR(perf_file))
492 return perf_file;
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -0800493
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200494 event = perf_file->private_data;
495 ee = ERR_PTR(-EINVAL);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000496
497 attr = perf_event_attrs(event);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200498 if (IS_ERR(attr) || attr->inherit)
499 goto err_out;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000500
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200501 switch (attr->type) {
502 case PERF_TYPE_SOFTWARE:
503 if (attr->config != PERF_COUNT_SW_BPF_OUTPUT)
504 goto err_out;
505 /* fall-through */
506 case PERF_TYPE_RAW:
507 case PERF_TYPE_HARDWARE:
508 ee = bpf_event_entry_gen(perf_file, map_file);
509 if (ee)
510 return ee;
511 ee = ERR_PTR(-ENOMEM);
512 /* fall-through */
513 default:
514 break;
515 }
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700516
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200517err_out:
518 fput(perf_file);
519 return ee;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000520}
521
522static void perf_event_fd_array_put_ptr(void *ptr)
523{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200524 bpf_event_entry_free_rcu(ptr);
525}
526
527static void perf_event_fd_array_release(struct bpf_map *map,
528 struct file *map_file)
529{
530 struct bpf_array *array = container_of(map, struct bpf_array, map);
531 struct bpf_event_entry *ee;
532 int i;
533
534 rcu_read_lock();
535 for (i = 0; i < array->map.max_entries; i++) {
536 ee = READ_ONCE(array->ptrs[i]);
537 if (ee && ee->map_file == map_file)
538 fd_array_map_delete_elem(map, &i);
539 }
540 rcu_read_unlock();
Kaixu Xiaea317b22015-08-06 07:02:34 +0000541}
542
543static const struct bpf_map_ops perf_event_array_ops = {
544 .map_alloc = fd_array_map_alloc,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200545 .map_free = fd_array_map_free,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000546 .map_get_next_key = array_map_get_next_key,
547 .map_lookup_elem = fd_array_map_lookup_elem,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000548 .map_delete_elem = fd_array_map_delete_elem,
549 .map_fd_get_ptr = perf_event_fd_array_get_ptr,
550 .map_fd_put_ptr = perf_event_fd_array_put_ptr,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200551 .map_release = perf_event_fd_array_release,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000552};
553
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +0100554static struct bpf_map_type_list perf_event_array_type __ro_after_init = {
Kaixu Xiaea317b22015-08-06 07:02:34 +0000555 .ops = &perf_event_array_ops,
556 .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
557};
558
559static int __init register_perf_event_array_map(void)
560{
561 bpf_register_map_type(&perf_event_array_type);
562 return 0;
563}
564late_initcall(register_perf_event_array_map);
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700565
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700566#ifdef CONFIG_CGROUPS
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700567static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
568 struct file *map_file /* not used */,
569 int fd)
570{
571 return cgroup_get_from_fd(fd);
572}
573
574static void cgroup_fd_array_put_ptr(void *ptr)
575{
576 /* cgroup_put free cgrp after a rcu grace period */
577 cgroup_put(ptr);
578}
579
580static void cgroup_fd_array_free(struct bpf_map *map)
581{
582 bpf_fd_array_map_clear(map);
583 fd_array_map_free(map);
584}
585
586static const struct bpf_map_ops cgroup_array_ops = {
587 .map_alloc = fd_array_map_alloc,
588 .map_free = cgroup_fd_array_free,
589 .map_get_next_key = array_map_get_next_key,
590 .map_lookup_elem = fd_array_map_lookup_elem,
591 .map_delete_elem = fd_array_map_delete_elem,
592 .map_fd_get_ptr = cgroup_fd_array_get_ptr,
593 .map_fd_put_ptr = cgroup_fd_array_put_ptr,
594};
595
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +0100596static struct bpf_map_type_list cgroup_array_type __ro_after_init = {
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700597 .ops = &cgroup_array_ops,
598 .type = BPF_MAP_TYPE_CGROUP_ARRAY,
599};
600
601static int __init register_cgroup_array_map(void)
602{
603 bpf_register_map_type(&cgroup_array_type);
604 return 0;
605}
606late_initcall(register_cgroup_array_map);
607#endif