blob: 075ddb80142298d4ebc0551367f692f118dcb3d5 [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);
Daniel Borkmann422baf62018-03-08 16:17:33 +010051 u64 cost, array_size, mask64;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080052 struct bpf_array *array;
Daniel Borkmann422baf62018-03-08 16:17:33 +010053 int ret;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080054
55 /* check sanity of attributes */
56 if (attr->max_entries == 0 || attr->key_size != 4 ||
Alexei Starovoitov823707b2016-03-07 21:57:16 -080057 attr->value_size == 0 || attr->map_flags)
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080058 return ERR_PTR(-EINVAL);
59
Alexei Starovoitov01b3f522015-11-29 16:59:35 -080060 if (attr->value_size >= 1 << (KMALLOC_SHIFT_MAX - 1))
61 /* if value_size is bigger, the user space won't be able to
62 * access the elements.
63 */
64 return ERR_PTR(-E2BIG);
65
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080066 elem_size = round_up(attr->value_size, 8);
67
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080068 max_entries = attr->max_entries;
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080069
Daniel Borkmann820ef2a2018-01-10 23:25:05 +010070 /* On 32 bit archs roundup_pow_of_two() with max_entries that has
71 * upper most bit set in u32 space is undefined behavior due to
72 * resulting 1U << 32, so do it manually here in u64 space.
73 */
74 mask64 = fls_long(max_entries - 1);
75 mask64 = 1ULL << mask64;
76 mask64 -= 1;
77
78 index_mask = mask64;
79 if (unpriv) {
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080080 /* round up array size to nearest power of 2,
81 * since cpu will speculate within index_mask limits
82 */
83 max_entries = index_mask + 1;
Daniel Borkmann820ef2a2018-01-10 23:25:05 +010084 /* Check for overflows. */
85 if (max_entries < attr->max_entries)
86 return ERR_PTR(-E2BIG);
87 }
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080088
Alexei Starovoitova10423b2016-02-01 22:39:54 -080089 array_size = sizeof(*array);
90 if (percpu)
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080091 array_size += (u64) max_entries * sizeof(void *);
Alexei Starovoitova10423b2016-02-01 22:39:54 -080092 else
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080093 array_size += (u64) max_entries * elem_size;
Alexei Starovoitova10423b2016-02-01 22:39:54 -080094
95 /* make sure there is no u32 overflow later in round_up() */
Daniel Borkmann422baf62018-03-08 16:17:33 +010096 cost = array_size;
97 if (cost >= U32_MAX - PAGE_SIZE)
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -080098 return ERR_PTR(-ENOMEM);
Daniel Borkmann422baf62018-03-08 16:17:33 +010099 if (percpu) {
100 cost += (u64)attr->max_entries * elem_size * num_possible_cpus();
101 if (cost >= U32_MAX - PAGE_SIZE)
102 return ERR_PTR(-ENOMEM);
103 }
104 cost = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
105
106 ret = bpf_map_precharge_memlock(cost);
107 if (ret < 0)
108 return ERR_PTR(ret);
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800109
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800110 /* allocate all map elements and zero-initialize them */
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100111 array = bpf_map_area_alloc(array_size);
112 if (!array)
113 return ERR_PTR(-ENOMEM);
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800114 array->index_mask = index_mask;
115 array->map.unpriv_array = unpriv;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800116
117 /* copy mandatory map attributes */
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800118 array->map.map_type = attr->map_type;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800119 array->map.key_size = attr->key_size;
120 array->map.value_size = attr->value_size;
121 array->map.max_entries = attr->max_entries;
Daniel Borkmann816cfeb2018-03-08 16:17:32 +0100122 array->map.map_flags = attr->map_flags;
Daniel Borkmann422baf62018-03-08 16:17:33 +0100123 array->map.pages = cost;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800124 array->elem_size = elem_size;
125
Daniel Borkmann422baf62018-03-08 16:17:33 +0100126 if (percpu &&
127 (elem_size > PCPU_MIN_UNIT_SIZE ||
128 bpf_array_alloc_percpu(array))) {
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100129 bpf_map_area_free(array);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800130 return ERR_PTR(-ENOMEM);
131 }
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800132
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800133 return &array->map;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800134}
135
136/* Called from syscall or from eBPF program */
137static void *array_map_lookup_elem(struct bpf_map *map, void *key)
138{
139 struct bpf_array *array = container_of(map, struct bpf_array, map);
140 u32 index = *(u32 *)key;
141
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800142 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800143 return NULL;
144
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800145 return array->value + array->elem_size * (index & array->index_mask);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800146}
147
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800148/* Called from eBPF program */
149static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
150{
151 struct bpf_array *array = container_of(map, struct bpf_array, map);
152 u32 index = *(u32 *)key;
153
154 if (unlikely(index >= array->map.max_entries))
155 return NULL;
156
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800157 return this_cpu_ptr(array->pptrs[index & array->index_mask]);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800158}
159
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800160int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
161{
162 struct bpf_array *array = container_of(map, struct bpf_array, map);
163 u32 index = *(u32 *)key;
164 void __percpu *pptr;
165 int cpu, off = 0;
166 u32 size;
167
168 if (unlikely(index >= array->map.max_entries))
169 return -ENOENT;
170
171 /* per_cpu areas are zero-filled and bpf programs can only
172 * access 'value_size' of them, so copying rounded areas
173 * will not leak any kernel data
174 */
175 size = round_up(map->value_size, 8);
176 rcu_read_lock();
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800177 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800178 for_each_possible_cpu(cpu) {
179 bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
180 off += size;
181 }
182 rcu_read_unlock();
183 return 0;
184}
185
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800186/* Called from syscall */
187static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
188{
189 struct bpf_array *array = container_of(map, struct bpf_array, map);
190 u32 index = *(u32 *)key;
191 u32 *next = (u32 *)next_key;
192
193 if (index >= array->map.max_entries) {
194 *next = 0;
195 return 0;
196 }
197
198 if (index == array->map.max_entries - 1)
199 return -ENOENT;
200
201 *next = index + 1;
202 return 0;
203}
204
205/* Called from syscall or from eBPF program */
206static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
207 u64 map_flags)
208{
209 struct bpf_array *array = container_of(map, struct bpf_array, map);
210 u32 index = *(u32 *)key;
211
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800212 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800213 /* unknown flags */
214 return -EINVAL;
215
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800216 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800217 /* all elements were pre-allocated, cannot insert a new one */
218 return -E2BIG;
219
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800220 if (unlikely(map_flags == BPF_NOEXIST))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800221 /* all elements already exist */
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800222 return -EEXIST;
223
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800224 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800225 memcpy(this_cpu_ptr(array->pptrs[index & array->index_mask]),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800226 value, map->value_size);
227 else
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800228 memcpy(array->value +
229 array->elem_size * (index & array->index_mask),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800230 value, map->value_size);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800231 return 0;
232}
233
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800234int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
235 u64 map_flags)
236{
237 struct bpf_array *array = container_of(map, struct bpf_array, map);
238 u32 index = *(u32 *)key;
239 void __percpu *pptr;
240 int cpu, off = 0;
241 u32 size;
242
243 if (unlikely(map_flags > BPF_EXIST))
244 /* unknown flags */
245 return -EINVAL;
246
247 if (unlikely(index >= array->map.max_entries))
248 /* all elements were pre-allocated, cannot insert a new one */
249 return -E2BIG;
250
251 if (unlikely(map_flags == BPF_NOEXIST))
252 /* all elements already exist */
253 return -EEXIST;
254
255 /* the user space will provide round_up(value_size, 8) bytes that
256 * will be copied into per-cpu area. bpf programs can only access
257 * value_size of it. During lookup the same extra bytes will be
258 * returned or zeros which were zero-filled by percpu_alloc,
259 * so no kernel data leaks possible
260 */
261 size = round_up(map->value_size, 8);
262 rcu_read_lock();
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800263 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800264 for_each_possible_cpu(cpu) {
265 bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
266 off += size;
267 }
268 rcu_read_unlock();
269 return 0;
270}
271
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800272/* Called from syscall or from eBPF program */
273static int array_map_delete_elem(struct bpf_map *map, void *key)
274{
275 return -EINVAL;
276}
277
278/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
279static void array_map_free(struct bpf_map *map)
280{
281 struct bpf_array *array = container_of(map, struct bpf_array, map);
282
283 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
284 * so the programs (can be more than one that used this map) were
285 * disconnected from events. Wait for outstanding programs to complete
286 * and free the array
287 */
288 synchronize_rcu();
289
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800290 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
291 bpf_array_free_percpu(array);
292
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100293 bpf_map_area_free(array);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800294}
295
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100296static const struct bpf_map_ops array_ops = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800297 .map_alloc = array_map_alloc,
298 .map_free = array_map_free,
299 .map_get_next_key = array_map_get_next_key,
300 .map_lookup_elem = array_map_lookup_elem,
301 .map_update_elem = array_map_update_elem,
302 .map_delete_elem = array_map_delete_elem,
303};
304
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100305static struct bpf_map_type_list array_type __read_mostly = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800306 .ops = &array_ops,
307 .type = BPF_MAP_TYPE_ARRAY,
308};
309
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800310static const struct bpf_map_ops percpu_array_ops = {
311 .map_alloc = array_map_alloc,
312 .map_free = array_map_free,
313 .map_get_next_key = array_map_get_next_key,
314 .map_lookup_elem = percpu_array_map_lookup_elem,
315 .map_update_elem = array_map_update_elem,
316 .map_delete_elem = array_map_delete_elem,
317};
318
319static struct bpf_map_type_list percpu_array_type __read_mostly = {
320 .ops = &percpu_array_ops,
321 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
322};
323
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800324static int __init register_array_map(void)
325{
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100326 bpf_register_map_type(&array_type);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800327 bpf_register_map_type(&percpu_array_type);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800328 return 0;
329}
330late_initcall(register_array_map);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700331
Wang Nan2a36f0b2015-08-06 07:02:33 +0000332static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700333{
Wang Nan2a36f0b2015-08-06 07:02:33 +0000334 /* only file descriptors can be stored in this type of map */
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700335 if (attr->value_size != sizeof(u32))
336 return ERR_PTR(-EINVAL);
337 return array_map_alloc(attr);
338}
339
Wang Nan2a36f0b2015-08-06 07:02:33 +0000340static void fd_array_map_free(struct bpf_map *map)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700341{
342 struct bpf_array *array = container_of(map, struct bpf_array, map);
343 int i;
344
345 synchronize_rcu();
346
347 /* make sure it's empty */
348 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000349 BUG_ON(array->ptrs[i] != NULL);
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100350
351 bpf_map_area_free(array);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700352}
353
Wang Nan2a36f0b2015-08-06 07:02:33 +0000354static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700355{
356 return NULL;
357}
358
359/* only called from syscall */
Daniel Borkmannd056a782016-06-15 22:47:13 +0200360int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
361 void *key, void *value, u64 map_flags)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700362{
363 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000364 void *new_ptr, *old_ptr;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700365 u32 index = *(u32 *)key, ufd;
366
367 if (map_flags != BPF_ANY)
368 return -EINVAL;
369
370 if (index >= array->map.max_entries)
371 return -E2BIG;
372
373 ufd = *(u32 *)value;
Daniel Borkmannd056a782016-06-15 22:47:13 +0200374 new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000375 if (IS_ERR(new_ptr))
376 return PTR_ERR(new_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700377
Wang Nan2a36f0b2015-08-06 07:02:33 +0000378 old_ptr = xchg(array->ptrs + index, new_ptr);
379 if (old_ptr)
380 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700381
382 return 0;
383}
384
Wang Nan2a36f0b2015-08-06 07:02:33 +0000385static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700386{
387 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000388 void *old_ptr;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700389 u32 index = *(u32 *)key;
390
391 if (index >= array->map.max_entries)
392 return -E2BIG;
393
Wang Nan2a36f0b2015-08-06 07:02:33 +0000394 old_ptr = xchg(array->ptrs + index, NULL);
395 if (old_ptr) {
396 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700397 return 0;
398 } else {
399 return -ENOENT;
400 }
401}
402
Daniel Borkmannd056a782016-06-15 22:47:13 +0200403static void *prog_fd_array_get_ptr(struct bpf_map *map,
404 struct file *map_file, int fd)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000405{
406 struct bpf_array *array = container_of(map, struct bpf_array, map);
407 struct bpf_prog *prog = bpf_prog_get(fd);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200408
Wang Nan2a36f0b2015-08-06 07:02:33 +0000409 if (IS_ERR(prog))
410 return prog;
411
412 if (!bpf_prog_array_compatible(array, prog)) {
413 bpf_prog_put(prog);
414 return ERR_PTR(-EINVAL);
415 }
Daniel Borkmannd056a782016-06-15 22:47:13 +0200416
Wang Nan2a36f0b2015-08-06 07:02:33 +0000417 return prog;
418}
419
420static void prog_fd_array_put_ptr(void *ptr)
421{
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200422 bpf_prog_put(ptr);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000423}
424
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700425/* decrement refcnt of all bpf_progs that are stored in this map */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000426void bpf_fd_array_map_clear(struct bpf_map *map)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700427{
428 struct bpf_array *array = container_of(map, struct bpf_array, map);
429 int i;
430
431 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000432 fd_array_map_delete_elem(map, &i);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700433}
434
435static const struct bpf_map_ops prog_array_ops = {
Wang Nan2a36f0b2015-08-06 07:02:33 +0000436 .map_alloc = fd_array_map_alloc,
437 .map_free = fd_array_map_free,
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700438 .map_get_next_key = array_map_get_next_key,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000439 .map_lookup_elem = fd_array_map_lookup_elem,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000440 .map_delete_elem = fd_array_map_delete_elem,
441 .map_fd_get_ptr = prog_fd_array_get_ptr,
442 .map_fd_put_ptr = prog_fd_array_put_ptr,
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700443};
444
445static struct bpf_map_type_list prog_array_type __read_mostly = {
446 .ops = &prog_array_ops,
447 .type = BPF_MAP_TYPE_PROG_ARRAY,
448};
449
450static int __init register_prog_array_map(void)
451{
452 bpf_register_map_type(&prog_array_type);
453 return 0;
454}
455late_initcall(register_prog_array_map);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000456
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200457static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
458 struct file *map_file)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000459{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200460 struct bpf_event_entry *ee;
461
Daniel Borkmann858d68f2016-07-16 01:15:55 +0200462 ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200463 if (ee) {
464 ee->event = perf_file->private_data;
465 ee->perf_file = perf_file;
466 ee->map_file = map_file;
467 }
468
469 return ee;
470}
471
472static void __bpf_event_entry_free(struct rcu_head *rcu)
473{
474 struct bpf_event_entry *ee;
475
476 ee = container_of(rcu, struct bpf_event_entry, rcu);
477 fput(ee->perf_file);
478 kfree(ee);
479}
480
481static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
482{
483 call_rcu(&ee->rcu, __bpf_event_entry_free);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000484}
485
Daniel Borkmannd056a782016-06-15 22:47:13 +0200486static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
487 struct file *map_file, int fd)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000488{
Kaixu Xiaea317b22015-08-06 07:02:34 +0000489 const struct perf_event_attr *attr;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200490 struct bpf_event_entry *ee;
491 struct perf_event *event;
492 struct file *perf_file;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000493
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200494 perf_file = perf_event_get(fd);
495 if (IS_ERR(perf_file))
496 return perf_file;
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -0800497
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200498 event = perf_file->private_data;
499 ee = ERR_PTR(-EINVAL);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000500
501 attr = perf_event_attrs(event);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200502 if (IS_ERR(attr) || attr->inherit)
503 goto err_out;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000504
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200505 switch (attr->type) {
506 case PERF_TYPE_SOFTWARE:
507 if (attr->config != PERF_COUNT_SW_BPF_OUTPUT)
508 goto err_out;
509 /* fall-through */
510 case PERF_TYPE_RAW:
511 case PERF_TYPE_HARDWARE:
512 ee = bpf_event_entry_gen(perf_file, map_file);
513 if (ee)
514 return ee;
515 ee = ERR_PTR(-ENOMEM);
516 /* fall-through */
517 default:
518 break;
519 }
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700520
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200521err_out:
522 fput(perf_file);
523 return ee;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000524}
525
526static void perf_event_fd_array_put_ptr(void *ptr)
527{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200528 bpf_event_entry_free_rcu(ptr);
529}
530
531static void perf_event_fd_array_release(struct bpf_map *map,
532 struct file *map_file)
533{
534 struct bpf_array *array = container_of(map, struct bpf_array, map);
535 struct bpf_event_entry *ee;
536 int i;
537
538 rcu_read_lock();
539 for (i = 0; i < array->map.max_entries; i++) {
540 ee = READ_ONCE(array->ptrs[i]);
541 if (ee && ee->map_file == map_file)
542 fd_array_map_delete_elem(map, &i);
543 }
544 rcu_read_unlock();
Kaixu Xiaea317b22015-08-06 07:02:34 +0000545}
546
547static const struct bpf_map_ops perf_event_array_ops = {
548 .map_alloc = fd_array_map_alloc,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200549 .map_free = fd_array_map_free,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000550 .map_get_next_key = array_map_get_next_key,
551 .map_lookup_elem = fd_array_map_lookup_elem,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000552 .map_delete_elem = fd_array_map_delete_elem,
553 .map_fd_get_ptr = perf_event_fd_array_get_ptr,
554 .map_fd_put_ptr = perf_event_fd_array_put_ptr,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200555 .map_release = perf_event_fd_array_release,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000556};
557
558static struct bpf_map_type_list perf_event_array_type __read_mostly = {
559 .ops = &perf_event_array_ops,
560 .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
561};
562
563static int __init register_perf_event_array_map(void)
564{
565 bpf_register_map_type(&perf_event_array_type);
566 return 0;
567}
568late_initcall(register_perf_event_array_map);
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700569
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700570#ifdef CONFIG_CGROUPS
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700571static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
572 struct file *map_file /* not used */,
573 int fd)
574{
575 return cgroup_get_from_fd(fd);
576}
577
578static void cgroup_fd_array_put_ptr(void *ptr)
579{
580 /* cgroup_put free cgrp after a rcu grace period */
581 cgroup_put(ptr);
582}
583
584static void cgroup_fd_array_free(struct bpf_map *map)
585{
586 bpf_fd_array_map_clear(map);
587 fd_array_map_free(map);
588}
589
590static const struct bpf_map_ops cgroup_array_ops = {
591 .map_alloc = fd_array_map_alloc,
592 .map_free = cgroup_fd_array_free,
593 .map_get_next_key = array_map_get_next_key,
594 .map_lookup_elem = fd_array_map_lookup_elem,
595 .map_delete_elem = fd_array_map_delete_elem,
596 .map_fd_get_ptr = cgroup_fd_array_get_ptr,
597 .map_fd_put_ptr = cgroup_fd_array_put_ptr,
598};
599
600static struct bpf_map_type_list cgroup_array_type __read_mostly = {
601 .ops = &cgroup_array_ops,
602 .type = BPF_MAP_TYPE_CGROUP_ARRAY,
603};
604
605static int __init register_cgroup_array_map(void)
606{
607 bpf_register_map_type(&cgroup_array_type);
608 return 0;
609}
610late_initcall(register_cgroup_array_map);
611#endif