blob: 5e00b2333c26e3256a6f4dc51a2a58011a2c9db6 [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
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070020#include "map_in_map.h"
21
Alexei Starovoitova10423b2016-02-01 22:39:54 -080022static void bpf_array_free_percpu(struct bpf_array *array)
23{
24 int i;
25
26 for (i = 0; i < array->map.max_entries; i++)
27 free_percpu(array->pptrs[i]);
28}
29
30static int bpf_array_alloc_percpu(struct bpf_array *array)
31{
32 void __percpu *ptr;
33 int i;
34
35 for (i = 0; i < array->map.max_entries; i++) {
36 ptr = __alloc_percpu_gfp(array->elem_size, 8,
37 GFP_USER | __GFP_NOWARN);
38 if (!ptr) {
39 bpf_array_free_percpu(array);
40 return -ENOMEM;
41 }
42 array->pptrs[i] = ptr;
43 }
44
45 return 0;
46}
47
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080048/* Called from syscall */
49static struct bpf_map *array_map_alloc(union bpf_attr *attr)
50{
Alexei Starovoitova10423b2016-02-01 22:39:54 -080051 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080052 struct bpf_array *array;
Alexei Starovoitova10423b2016-02-01 22:39:54 -080053 u64 array_size;
54 u32 elem_size;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080055
56 /* check sanity of attributes */
57 if (attr->max_entries == 0 || attr->key_size != 4 ||
Alexei Starovoitov823707b2016-03-07 21:57:16 -080058 attr->value_size == 0 || attr->map_flags)
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080059 return ERR_PTR(-EINVAL);
60
Michal Hocko7984c272017-01-10 16:57:30 -080061 if (attr->value_size > KMALLOC_MAX_SIZE)
Alexei Starovoitov01b3f522015-11-29 16:59:35 -080062 /* if value_size is bigger, the user space won't be able to
63 * access the elements.
64 */
65 return ERR_PTR(-E2BIG);
66
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080067 elem_size = round_up(attr->value_size, 8);
68
Alexei Starovoitova10423b2016-02-01 22:39:54 -080069 array_size = sizeof(*array);
70 if (percpu)
71 array_size += (u64) attr->max_entries * sizeof(void *);
72 else
73 array_size += (u64) attr->max_entries * elem_size;
74
75 /* make sure there is no u32 overflow later in round_up() */
76 if (array_size >= U32_MAX - PAGE_SIZE)
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -080077 return ERR_PTR(-ENOMEM);
78
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080079 /* allocate all map elements and zero-initialize them */
Daniel Borkmannd407bd22017-01-18 15:14:17 +010080 array = bpf_map_area_alloc(array_size);
81 if (!array)
82 return ERR_PTR(-ENOMEM);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080083
84 /* copy mandatory map attributes */
Alexei Starovoitova10423b2016-02-01 22:39:54 -080085 array->map.map_type = attr->map_type;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080086 array->map.key_size = attr->key_size;
87 array->map.value_size = attr->value_size;
88 array->map.max_entries = attr->max_entries;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080089 array->elem_size = elem_size;
90
Alexei Starovoitova10423b2016-02-01 22:39:54 -080091 if (!percpu)
92 goto out;
93
94 array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
95
96 if (array_size >= U32_MAX - PAGE_SIZE ||
97 elem_size > PCPU_MIN_UNIT_SIZE || bpf_array_alloc_percpu(array)) {
Daniel Borkmannd407bd22017-01-18 15:14:17 +010098 bpf_map_area_free(array);
Alexei Starovoitova10423b2016-02-01 22:39:54 -080099 return ERR_PTR(-ENOMEM);
100 }
101out:
102 array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
103
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800104 return &array->map;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800105}
106
107/* Called from syscall or from eBPF program */
108static void *array_map_lookup_elem(struct bpf_map *map, void *key)
109{
110 struct bpf_array *array = container_of(map, struct bpf_array, map);
111 u32 index = *(u32 *)key;
112
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800113 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800114 return NULL;
115
116 return array->value + array->elem_size * index;
117}
118
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700119/* emit BPF instructions equivalent to C code of array_map_lookup_elem() */
120static u32 array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
121{
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700122 struct bpf_insn *insn = insn_buf;
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700123 u32 elem_size = round_up(map->value_size, 8);
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700124 const int ret = BPF_REG_0;
125 const int map_ptr = BPF_REG_1;
126 const int index = BPF_REG_2;
127
128 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
129 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700130 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3);
131
132 if (is_power_of_2(elem_size)) {
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700133 *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
134 } else {
135 *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
136 }
137 *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
138 *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
139 *insn++ = BPF_MOV64_IMM(ret, 0);
140 return insn - insn_buf;
141}
142
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800143/* Called from eBPF program */
144static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
145{
146 struct bpf_array *array = container_of(map, struct bpf_array, map);
147 u32 index = *(u32 *)key;
148
149 if (unlikely(index >= array->map.max_entries))
150 return NULL;
151
152 return this_cpu_ptr(array->pptrs[index]);
153}
154
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800155int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
156{
157 struct bpf_array *array = container_of(map, struct bpf_array, map);
158 u32 index = *(u32 *)key;
159 void __percpu *pptr;
160 int cpu, off = 0;
161 u32 size;
162
163 if (unlikely(index >= array->map.max_entries))
164 return -ENOENT;
165
166 /* per_cpu areas are zero-filled and bpf programs can only
167 * access 'value_size' of them, so copying rounded areas
168 * will not leak any kernel data
169 */
170 size = round_up(map->value_size, 8);
171 rcu_read_lock();
172 pptr = array->pptrs[index];
173 for_each_possible_cpu(cpu) {
174 bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
175 off += size;
176 }
177 rcu_read_unlock();
178 return 0;
179}
180
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800181/* Called from syscall */
182static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
183{
184 struct bpf_array *array = container_of(map, struct bpf_array, map);
Teng Qin8fe45922017-04-24 19:00:37 -0700185 u32 index = key ? *(u32 *)key : U32_MAX;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800186 u32 *next = (u32 *)next_key;
187
188 if (index >= array->map.max_entries) {
189 *next = 0;
190 return 0;
191 }
192
193 if (index == array->map.max_entries - 1)
194 return -ENOENT;
195
196 *next = index + 1;
197 return 0;
198}
199
200/* Called from syscall or from eBPF program */
201static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
202 u64 map_flags)
203{
204 struct bpf_array *array = container_of(map, struct bpf_array, map);
205 u32 index = *(u32 *)key;
206
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800207 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800208 /* unknown flags */
209 return -EINVAL;
210
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800211 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800212 /* all elements were pre-allocated, cannot insert a new one */
213 return -E2BIG;
214
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800215 if (unlikely(map_flags == BPF_NOEXIST))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800216 /* all elements already exist */
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800217 return -EEXIST;
218
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800219 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
220 memcpy(this_cpu_ptr(array->pptrs[index]),
221 value, map->value_size);
222 else
223 memcpy(array->value + array->elem_size * index,
224 value, map->value_size);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800225 return 0;
226}
227
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800228int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
229 u64 map_flags)
230{
231 struct bpf_array *array = container_of(map, struct bpf_array, map);
232 u32 index = *(u32 *)key;
233 void __percpu *pptr;
234 int cpu, off = 0;
235 u32 size;
236
237 if (unlikely(map_flags > BPF_EXIST))
238 /* unknown flags */
239 return -EINVAL;
240
241 if (unlikely(index >= array->map.max_entries))
242 /* all elements were pre-allocated, cannot insert a new one */
243 return -E2BIG;
244
245 if (unlikely(map_flags == BPF_NOEXIST))
246 /* all elements already exist */
247 return -EEXIST;
248
249 /* the user space will provide round_up(value_size, 8) bytes that
250 * will be copied into per-cpu area. bpf programs can only access
251 * value_size of it. During lookup the same extra bytes will be
252 * returned or zeros which were zero-filled by percpu_alloc,
253 * so no kernel data leaks possible
254 */
255 size = round_up(map->value_size, 8);
256 rcu_read_lock();
257 pptr = array->pptrs[index];
258 for_each_possible_cpu(cpu) {
259 bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
260 off += size;
261 }
262 rcu_read_unlock();
263 return 0;
264}
265
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800266/* Called from syscall or from eBPF program */
267static int array_map_delete_elem(struct bpf_map *map, void *key)
268{
269 return -EINVAL;
270}
271
272/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
273static void array_map_free(struct bpf_map *map)
274{
275 struct bpf_array *array = container_of(map, struct bpf_array, map);
276
277 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
278 * so the programs (can be more than one that used this map) were
279 * disconnected from events. Wait for outstanding programs to complete
280 * and free the array
281 */
282 synchronize_rcu();
283
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800284 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
285 bpf_array_free_percpu(array);
286
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100287 bpf_map_area_free(array);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800288}
289
Johannes Berg40077e02017-04-11 15:34:58 +0200290const struct bpf_map_ops array_map_ops = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800291 .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 = array_map_lookup_elem,
295 .map_update_elem = array_map_update_elem,
296 .map_delete_elem = array_map_delete_elem,
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700297 .map_gen_lookup = array_map_gen_lookup,
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800298};
299
Johannes Berg40077e02017-04-11 15:34:58 +0200300const struct bpf_map_ops percpu_array_map_ops = {
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800301 .map_alloc = array_map_alloc,
302 .map_free = array_map_free,
303 .map_get_next_key = array_map_get_next_key,
304 .map_lookup_elem = percpu_array_map_lookup_elem,
305 .map_update_elem = array_map_update_elem,
306 .map_delete_elem = array_map_delete_elem,
307};
308
Wang Nan2a36f0b2015-08-06 07:02:33 +0000309static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700310{
Wang Nan2a36f0b2015-08-06 07:02:33 +0000311 /* only file descriptors can be stored in this type of map */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700312 if (attr->value_size != sizeof(u32))
313 return ERR_PTR(-EINVAL);
314 return array_map_alloc(attr);
315}
316
Wang Nan2a36f0b2015-08-06 07:02:33 +0000317static void fd_array_map_free(struct bpf_map *map)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700318{
319 struct bpf_array *array = container_of(map, struct bpf_array, map);
320 int i;
321
322 synchronize_rcu();
323
324 /* make sure it's empty */
325 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000326 BUG_ON(array->ptrs[i] != NULL);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100327
328 bpf_map_area_free(array);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700329}
330
Wang Nan2a36f0b2015-08-06 07:02:33 +0000331static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700332{
333 return NULL;
334}
335
336/* only called from syscall */
Daniel Borkmannd056a782016-06-15 22:47:13 +0200337int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
338 void *key, void *value, u64 map_flags)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700339{
340 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000341 void *new_ptr, *old_ptr;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700342 u32 index = *(u32 *)key, ufd;
343
344 if (map_flags != BPF_ANY)
345 return -EINVAL;
346
347 if (index >= array->map.max_entries)
348 return -E2BIG;
349
350 ufd = *(u32 *)value;
Daniel Borkmannd056a782016-06-15 22:47:13 +0200351 new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000352 if (IS_ERR(new_ptr))
353 return PTR_ERR(new_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700354
Wang Nan2a36f0b2015-08-06 07:02:33 +0000355 old_ptr = xchg(array->ptrs + index, new_ptr);
356 if (old_ptr)
357 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700358
359 return 0;
360}
361
Wang Nan2a36f0b2015-08-06 07:02:33 +0000362static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700363{
364 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000365 void *old_ptr;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700366 u32 index = *(u32 *)key;
367
368 if (index >= array->map.max_entries)
369 return -E2BIG;
370
Wang Nan2a36f0b2015-08-06 07:02:33 +0000371 old_ptr = xchg(array->ptrs + index, NULL);
372 if (old_ptr) {
373 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700374 return 0;
375 } else {
376 return -ENOENT;
377 }
378}
379
Daniel Borkmannd056a782016-06-15 22:47:13 +0200380static void *prog_fd_array_get_ptr(struct bpf_map *map,
381 struct file *map_file, int fd)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000382{
383 struct bpf_array *array = container_of(map, struct bpf_array, map);
384 struct bpf_prog *prog = bpf_prog_get(fd);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200385
Wang Nan2a36f0b2015-08-06 07:02:33 +0000386 if (IS_ERR(prog))
387 return prog;
388
389 if (!bpf_prog_array_compatible(array, prog)) {
390 bpf_prog_put(prog);
391 return ERR_PTR(-EINVAL);
392 }
Daniel Borkmannd056a782016-06-15 22:47:13 +0200393
Wang Nan2a36f0b2015-08-06 07:02:33 +0000394 return prog;
395}
396
397static void prog_fd_array_put_ptr(void *ptr)
398{
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200399 bpf_prog_put(ptr);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000400}
401
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700402/* decrement refcnt of all bpf_progs that are stored in this map */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000403void bpf_fd_array_map_clear(struct bpf_map *map)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700404{
405 struct bpf_array *array = container_of(map, struct bpf_array, map);
406 int i;
407
408 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000409 fd_array_map_delete_elem(map, &i);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700410}
411
Johannes Berg40077e02017-04-11 15:34:58 +0200412const struct bpf_map_ops prog_array_map_ops = {
Wang Nan2a36f0b2015-08-06 07:02:33 +0000413 .map_alloc = fd_array_map_alloc,
414 .map_free = fd_array_map_free,
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700415 .map_get_next_key = array_map_get_next_key,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000416 .map_lookup_elem = fd_array_map_lookup_elem,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000417 .map_delete_elem = fd_array_map_delete_elem,
418 .map_fd_get_ptr = prog_fd_array_get_ptr,
419 .map_fd_put_ptr = prog_fd_array_put_ptr,
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700420};
421
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200422static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
423 struct file *map_file)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000424{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200425 struct bpf_event_entry *ee;
426
Daniel Borkmann858d68f2016-07-16 01:15:55 +0200427 ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200428 if (ee) {
429 ee->event = perf_file->private_data;
430 ee->perf_file = perf_file;
431 ee->map_file = map_file;
432 }
433
434 return ee;
435}
436
437static void __bpf_event_entry_free(struct rcu_head *rcu)
438{
439 struct bpf_event_entry *ee;
440
441 ee = container_of(rcu, struct bpf_event_entry, rcu);
442 fput(ee->perf_file);
443 kfree(ee);
444}
445
446static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
447{
448 call_rcu(&ee->rcu, __bpf_event_entry_free);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000449}
450
Daniel Borkmannd056a782016-06-15 22:47:13 +0200451static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
452 struct file *map_file, int fd)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000453{
Kaixu Xiaea317b22015-08-06 07:02:34 +0000454 const struct perf_event_attr *attr;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200455 struct bpf_event_entry *ee;
456 struct perf_event *event;
457 struct file *perf_file;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000458
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200459 perf_file = perf_event_get(fd);
460 if (IS_ERR(perf_file))
461 return perf_file;
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -0800462
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200463 event = perf_file->private_data;
464 ee = ERR_PTR(-EINVAL);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000465
466 attr = perf_event_attrs(event);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200467 if (IS_ERR(attr) || attr->inherit)
468 goto err_out;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000469
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200470 switch (attr->type) {
471 case PERF_TYPE_SOFTWARE:
472 if (attr->config != PERF_COUNT_SW_BPF_OUTPUT)
473 goto err_out;
474 /* fall-through */
475 case PERF_TYPE_RAW:
476 case PERF_TYPE_HARDWARE:
477 ee = bpf_event_entry_gen(perf_file, map_file);
478 if (ee)
479 return ee;
480 ee = ERR_PTR(-ENOMEM);
481 /* fall-through */
482 default:
483 break;
484 }
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700485
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200486err_out:
487 fput(perf_file);
488 return ee;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000489}
490
491static void perf_event_fd_array_put_ptr(void *ptr)
492{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200493 bpf_event_entry_free_rcu(ptr);
494}
495
496static void perf_event_fd_array_release(struct bpf_map *map,
497 struct file *map_file)
498{
499 struct bpf_array *array = container_of(map, struct bpf_array, map);
500 struct bpf_event_entry *ee;
501 int i;
502
503 rcu_read_lock();
504 for (i = 0; i < array->map.max_entries; i++) {
505 ee = READ_ONCE(array->ptrs[i]);
506 if (ee && ee->map_file == map_file)
507 fd_array_map_delete_elem(map, &i);
508 }
509 rcu_read_unlock();
Kaixu Xiaea317b22015-08-06 07:02:34 +0000510}
511
Johannes Berg40077e02017-04-11 15:34:58 +0200512const struct bpf_map_ops perf_event_array_map_ops = {
Kaixu Xiaea317b22015-08-06 07:02:34 +0000513 .map_alloc = fd_array_map_alloc,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200514 .map_free = fd_array_map_free,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000515 .map_get_next_key = array_map_get_next_key,
516 .map_lookup_elem = fd_array_map_lookup_elem,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000517 .map_delete_elem = fd_array_map_delete_elem,
518 .map_fd_get_ptr = perf_event_fd_array_get_ptr,
519 .map_fd_put_ptr = perf_event_fd_array_put_ptr,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200520 .map_release = perf_event_fd_array_release,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000521};
522
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700523#ifdef CONFIG_CGROUPS
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700524static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
525 struct file *map_file /* not used */,
526 int fd)
527{
528 return cgroup_get_from_fd(fd);
529}
530
531static void cgroup_fd_array_put_ptr(void *ptr)
532{
533 /* cgroup_put free cgrp after a rcu grace period */
534 cgroup_put(ptr);
535}
536
537static void cgroup_fd_array_free(struct bpf_map *map)
538{
539 bpf_fd_array_map_clear(map);
540 fd_array_map_free(map);
541}
542
Johannes Berg40077e02017-04-11 15:34:58 +0200543const struct bpf_map_ops cgroup_array_map_ops = {
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700544 .map_alloc = fd_array_map_alloc,
545 .map_free = cgroup_fd_array_free,
546 .map_get_next_key = array_map_get_next_key,
547 .map_lookup_elem = fd_array_map_lookup_elem,
548 .map_delete_elem = fd_array_map_delete_elem,
549 .map_fd_get_ptr = cgroup_fd_array_get_ptr,
550 .map_fd_put_ptr = cgroup_fd_array_put_ptr,
551};
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700552#endif
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700553
554static struct bpf_map *array_of_map_alloc(union bpf_attr *attr)
555{
556 struct bpf_map *map, *inner_map_meta;
557
558 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
559 if (IS_ERR(inner_map_meta))
560 return inner_map_meta;
561
562 map = fd_array_map_alloc(attr);
563 if (IS_ERR(map)) {
564 bpf_map_meta_free(inner_map_meta);
565 return map;
566 }
567
568 map->inner_map_meta = inner_map_meta;
569
570 return map;
571}
572
573static void array_of_map_free(struct bpf_map *map)
574{
575 /* map->inner_map_meta is only accessed by syscall which
576 * is protected by fdget/fdput.
577 */
578 bpf_map_meta_free(map->inner_map_meta);
579 bpf_fd_array_map_clear(map);
580 fd_array_map_free(map);
581}
582
583static void *array_of_map_lookup_elem(struct bpf_map *map, void *key)
584{
585 struct bpf_map **inner_map = array_map_lookup_elem(map, key);
586
587 if (!inner_map)
588 return NULL;
589
590 return READ_ONCE(*inner_map);
591}
592
Johannes Berg40077e02017-04-11 15:34:58 +0200593const struct bpf_map_ops array_of_maps_map_ops = {
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700594 .map_alloc = array_of_map_alloc,
595 .map_free = array_of_map_free,
596 .map_get_next_key = array_map_get_next_key,
597 .map_lookup_elem = array_of_map_lookup_elem,
598 .map_delete_elem = fd_array_map_delete_elem,
599 .map_fd_get_ptr = bpf_map_fd_get_ptr,
600 .map_fd_put_ptr = bpf_map_fd_put_ptr,
601};