blob: b30ca0f2fa41fcc162a0bc2704c051bd10a7a341 [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
Chenbo Feng4672ded2017-10-18 13:00:22 -070019#define ARRAY_CREATE_FLAG_MASK \
20 (BPF_F_RDONLY | BPF_F_WRONLY)
21
Alexei Starovoitova10423b2016-02-01 22:39:54 -080022static void bpf_array_free_percpu(struct bpf_array *array)
23{
24 int i;
25
Eric Dumazet2a8bc532018-03-08 16:17:36 +010026 for (i = 0; i < array->map.max_entries; i++) {
Alexei Starovoitova10423b2016-02-01 22:39:54 -080027 free_percpu(array->pptrs[i]);
Eric Dumazet2a8bc532018-03-08 16:17:36 +010028 cond_resched();
29 }
Alexei Starovoitova10423b2016-02-01 22:39:54 -080030}
31
32static int bpf_array_alloc_percpu(struct bpf_array *array)
33{
34 void __percpu *ptr;
35 int i;
36
37 for (i = 0; i < array->map.max_entries; i++) {
38 ptr = __alloc_percpu_gfp(array->elem_size, 8,
39 GFP_USER | __GFP_NOWARN);
40 if (!ptr) {
41 bpf_array_free_percpu(array);
42 return -ENOMEM;
43 }
44 array->pptrs[i] = ptr;
Eric Dumazet2a8bc532018-03-08 16:17:36 +010045 cond_resched();
Alexei Starovoitova10423b2016-02-01 22:39:54 -080046 }
47
48 return 0;
49}
50
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080051/* Called from syscall */
52static struct bpf_map *array_map_alloc(union bpf_attr *attr)
53{
Alexei Starovoitova10423b2016-02-01 22:39:54 -080054 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080055 u32 elem_size, index_mask, max_entries;
56 bool unpriv = !capable(CAP_SYS_ADMIN);
Daniel Borkmann422baf62018-03-08 16:17:33 +010057 u64 cost, array_size, mask64;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080058 struct bpf_array *array;
Daniel Borkmann422baf62018-03-08 16:17:33 +010059 int ret;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080060
61 /* check sanity of attributes */
62 if (attr->max_entries == 0 || attr->key_size != 4 ||
Chenbo Feng4672ded2017-10-18 13:00:22 -070063 attr->value_size == 0 ||
64 attr->map_flags & ~ARRAY_CREATE_FLAG_MASK)
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080065 return ERR_PTR(-EINVAL);
66
Alexei Starovoitov01b3f522015-11-29 16:59:35 -080067 if (attr->value_size >= 1 << (KMALLOC_SHIFT_MAX - 1))
68 /* if value_size is bigger, the user space won't be able to
69 * access the elements.
70 */
71 return ERR_PTR(-E2BIG);
72
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080073 elem_size = round_up(attr->value_size, 8);
74
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080075 max_entries = attr->max_entries;
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080076
Daniel Borkmann820ef2a2018-01-10 23:25:05 +010077 /* On 32 bit archs roundup_pow_of_two() with max_entries that has
78 * upper most bit set in u32 space is undefined behavior due to
79 * resulting 1U << 32, so do it manually here in u64 space.
80 */
81 mask64 = fls_long(max_entries - 1);
82 mask64 = 1ULL << mask64;
83 mask64 -= 1;
84
85 index_mask = mask64;
86 if (unpriv) {
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080087 /* round up array size to nearest power of 2,
88 * since cpu will speculate within index_mask limits
89 */
90 max_entries = index_mask + 1;
Daniel Borkmann820ef2a2018-01-10 23:25:05 +010091 /* Check for overflows. */
92 if (max_entries < attr->max_entries)
93 return ERR_PTR(-E2BIG);
94 }
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080095
Alexei Starovoitova10423b2016-02-01 22:39:54 -080096 array_size = sizeof(*array);
97 if (percpu)
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080098 array_size += (u64) max_entries * sizeof(void *);
Alexei Starovoitova10423b2016-02-01 22:39:54 -080099 else
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800100 array_size += (u64) max_entries * elem_size;
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800101
102 /* make sure there is no u32 overflow later in round_up() */
Daniel Borkmann422baf62018-03-08 16:17:33 +0100103 cost = array_size;
104 if (cost >= U32_MAX - PAGE_SIZE)
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800105 return ERR_PTR(-ENOMEM);
Daniel Borkmann422baf62018-03-08 16:17:33 +0100106 if (percpu) {
107 cost += (u64)attr->max_entries * elem_size * num_possible_cpus();
108 if (cost >= U32_MAX - PAGE_SIZE)
109 return ERR_PTR(-ENOMEM);
110 }
111 cost = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
112
113 ret = bpf_map_precharge_memlock(cost);
114 if (ret < 0)
115 return ERR_PTR(ret);
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800116
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800117 /* allocate all map elements and zero-initialize them */
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100118 array = bpf_map_area_alloc(array_size);
119 if (!array)
120 return ERR_PTR(-ENOMEM);
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800121 array->index_mask = index_mask;
122 array->map.unpriv_array = unpriv;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800123
124 /* copy mandatory map attributes */
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800125 array->map.map_type = attr->map_type;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800126 array->map.key_size = attr->key_size;
127 array->map.value_size = attr->value_size;
128 array->map.max_entries = attr->max_entries;
Daniel Borkmann816cfeb2018-03-08 16:17:32 +0100129 array->map.map_flags = attr->map_flags;
Daniel Borkmann422baf62018-03-08 16:17:33 +0100130 array->map.pages = cost;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800131 array->elem_size = elem_size;
132
Daniel Borkmann422baf62018-03-08 16:17:33 +0100133 if (percpu &&
134 (elem_size > PCPU_MIN_UNIT_SIZE ||
135 bpf_array_alloc_percpu(array))) {
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100136 bpf_map_area_free(array);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800137 return ERR_PTR(-ENOMEM);
138 }
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800139
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800140 return &array->map;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800141}
142
143/* Called from syscall or from eBPF program */
144static void *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
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800149 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800150 return NULL;
151
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800152 return array->value + array->elem_size * (index & array->index_mask);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800153}
154
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800155/* Called from eBPF program */
156static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
157{
158 struct bpf_array *array = container_of(map, struct bpf_array, map);
159 u32 index = *(u32 *)key;
160
161 if (unlikely(index >= array->map.max_entries))
162 return NULL;
163
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800164 return this_cpu_ptr(array->pptrs[index & array->index_mask]);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800165}
166
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800167int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
168{
169 struct bpf_array *array = container_of(map, struct bpf_array, map);
170 u32 index = *(u32 *)key;
171 void __percpu *pptr;
172 int cpu, off = 0;
173 u32 size;
174
175 if (unlikely(index >= array->map.max_entries))
176 return -ENOENT;
177
178 /* per_cpu areas are zero-filled and bpf programs can only
179 * access 'value_size' of them, so copying rounded areas
180 * will not leak any kernel data
181 */
182 size = round_up(map->value_size, 8);
183 rcu_read_lock();
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800184 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800185 for_each_possible_cpu(cpu) {
186 bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
187 off += size;
188 }
189 rcu_read_unlock();
190 return 0;
191}
192
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800193/* Called from syscall */
194static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
195{
196 struct bpf_array *array = container_of(map, struct bpf_array, map);
Teng Qinfcbc8d02017-04-24 19:00:37 -0700197 u32 index = key ? *(u32 *)key : U32_MAX;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800198 u32 *next = (u32 *)next_key;
199
200 if (index >= array->map.max_entries) {
201 *next = 0;
202 return 0;
203 }
204
205 if (index == array->map.max_entries - 1)
206 return -ENOENT;
207
208 *next = index + 1;
209 return 0;
210}
211
212/* Called from syscall or from eBPF program */
213static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
214 u64 map_flags)
215{
216 struct bpf_array *array = container_of(map, struct bpf_array, map);
217 u32 index = *(u32 *)key;
218
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800219 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800220 /* unknown flags */
221 return -EINVAL;
222
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800223 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800224 /* all elements were pre-allocated, cannot insert a new one */
225 return -E2BIG;
226
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800227 if (unlikely(map_flags == BPF_NOEXIST))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800228 /* all elements already exist */
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800229 return -EEXIST;
230
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800231 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800232 memcpy(this_cpu_ptr(array->pptrs[index & array->index_mask]),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800233 value, map->value_size);
234 else
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800235 memcpy(array->value +
236 array->elem_size * (index & array->index_mask),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800237 value, map->value_size);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800238 return 0;
239}
240
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800241int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
242 u64 map_flags)
243{
244 struct bpf_array *array = container_of(map, struct bpf_array, map);
245 u32 index = *(u32 *)key;
246 void __percpu *pptr;
247 int cpu, off = 0;
248 u32 size;
249
250 if (unlikely(map_flags > BPF_EXIST))
251 /* unknown flags */
252 return -EINVAL;
253
254 if (unlikely(index >= array->map.max_entries))
255 /* all elements were pre-allocated, cannot insert a new one */
256 return -E2BIG;
257
258 if (unlikely(map_flags == BPF_NOEXIST))
259 /* all elements already exist */
260 return -EEXIST;
261
262 /* the user space will provide round_up(value_size, 8) bytes that
263 * will be copied into per-cpu area. bpf programs can only access
264 * value_size of it. During lookup the same extra bytes will be
265 * returned or zeros which were zero-filled by percpu_alloc,
266 * so no kernel data leaks possible
267 */
268 size = round_up(map->value_size, 8);
269 rcu_read_lock();
Alexei Starovoitova9bfac142018-01-07 17:33:02 -0800270 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800271 for_each_possible_cpu(cpu) {
272 bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
273 off += size;
274 }
275 rcu_read_unlock();
276 return 0;
277}
278
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800279/* Called from syscall or from eBPF program */
280static int array_map_delete_elem(struct bpf_map *map, void *key)
281{
282 return -EINVAL;
283}
284
285/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
286static void array_map_free(struct bpf_map *map)
287{
288 struct bpf_array *array = container_of(map, struct bpf_array, map);
289
290 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
291 * so the programs (can be more than one that used this map) were
292 * disconnected from events. Wait for outstanding programs to complete
293 * and free the array
294 */
295 synchronize_rcu();
296
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800297 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
298 bpf_array_free_percpu(array);
299
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100300 bpf_map_area_free(array);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800301}
302
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100303static const struct bpf_map_ops array_ops = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800304 .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 = array_map_lookup_elem,
308 .map_update_elem = array_map_update_elem,
309 .map_delete_elem = array_map_delete_elem,
310};
311
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100312static struct bpf_map_type_list array_type __read_mostly = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800313 .ops = &array_ops,
314 .type = BPF_MAP_TYPE_ARRAY,
315};
316
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800317static const struct bpf_map_ops percpu_array_ops = {
318 .map_alloc = array_map_alloc,
319 .map_free = array_map_free,
320 .map_get_next_key = array_map_get_next_key,
321 .map_lookup_elem = percpu_array_map_lookup_elem,
322 .map_update_elem = array_map_update_elem,
323 .map_delete_elem = array_map_delete_elem,
324};
325
326static struct bpf_map_type_list percpu_array_type __read_mostly = {
327 .ops = &percpu_array_ops,
328 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
329};
330
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800331static int __init register_array_map(void)
332{
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100333 bpf_register_map_type(&array_type);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800334 bpf_register_map_type(&percpu_array_type);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800335 return 0;
336}
337late_initcall(register_array_map);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700338
Wang Nan2a36f0b2015-08-06 07:02:33 +0000339static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700340{
Wang Nan2a36f0b2015-08-06 07:02:33 +0000341 /* only file descriptors can be stored in this type of map */
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700342 if (attr->value_size != sizeof(u32))
343 return ERR_PTR(-EINVAL);
344 return array_map_alloc(attr);
345}
346
Wang Nan2a36f0b2015-08-06 07:02:33 +0000347static void fd_array_map_free(struct bpf_map *map)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700348{
349 struct bpf_array *array = container_of(map, struct bpf_array, map);
350 int i;
351
352 synchronize_rcu();
353
354 /* make sure it's empty */
355 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000356 BUG_ON(array->ptrs[i] != NULL);
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100357
358 bpf_map_area_free(array);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700359}
360
Wang Nan2a36f0b2015-08-06 07:02:33 +0000361static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700362{
363 return NULL;
364}
365
366/* only called from syscall */
Daniel Borkmannd056a782016-06-15 22:47:13 +0200367int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
368 void *key, void *value, u64 map_flags)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700369{
370 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000371 void *new_ptr, *old_ptr;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700372 u32 index = *(u32 *)key, ufd;
373
374 if (map_flags != BPF_ANY)
375 return -EINVAL;
376
377 if (index >= array->map.max_entries)
378 return -E2BIG;
379
380 ufd = *(u32 *)value;
Daniel Borkmannd056a782016-06-15 22:47:13 +0200381 new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000382 if (IS_ERR(new_ptr))
383 return PTR_ERR(new_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700384
Wang Nan2a36f0b2015-08-06 07:02:33 +0000385 old_ptr = xchg(array->ptrs + index, new_ptr);
386 if (old_ptr)
387 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700388
389 return 0;
390}
391
Wang Nan2a36f0b2015-08-06 07:02:33 +0000392static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700393{
394 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000395 void *old_ptr;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700396 u32 index = *(u32 *)key;
397
398 if (index >= array->map.max_entries)
399 return -E2BIG;
400
Wang Nan2a36f0b2015-08-06 07:02:33 +0000401 old_ptr = xchg(array->ptrs + index, NULL);
402 if (old_ptr) {
403 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700404 return 0;
405 } else {
406 return -ENOENT;
407 }
408}
409
Daniel Borkmannd056a782016-06-15 22:47:13 +0200410static void *prog_fd_array_get_ptr(struct bpf_map *map,
411 struct file *map_file, int fd)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000412{
413 struct bpf_array *array = container_of(map, struct bpf_array, map);
414 struct bpf_prog *prog = bpf_prog_get(fd);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200415
Wang Nan2a36f0b2015-08-06 07:02:33 +0000416 if (IS_ERR(prog))
417 return prog;
418
419 if (!bpf_prog_array_compatible(array, prog)) {
420 bpf_prog_put(prog);
421 return ERR_PTR(-EINVAL);
422 }
Daniel Borkmannd056a782016-06-15 22:47:13 +0200423
Wang Nan2a36f0b2015-08-06 07:02:33 +0000424 return prog;
425}
426
427static void prog_fd_array_put_ptr(void *ptr)
428{
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200429 bpf_prog_put(ptr);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000430}
431
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700432/* decrement refcnt of all bpf_progs that are stored in this map */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000433void bpf_fd_array_map_clear(struct bpf_map *map)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700434{
435 struct bpf_array *array = container_of(map, struct bpf_array, map);
436 int i;
437
438 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000439 fd_array_map_delete_elem(map, &i);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700440}
441
442static const struct bpf_map_ops prog_array_ops = {
Wang Nan2a36f0b2015-08-06 07:02:33 +0000443 .map_alloc = fd_array_map_alloc,
444 .map_free = fd_array_map_free,
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700445 .map_get_next_key = array_map_get_next_key,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000446 .map_lookup_elem = fd_array_map_lookup_elem,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000447 .map_delete_elem = fd_array_map_delete_elem,
448 .map_fd_get_ptr = prog_fd_array_get_ptr,
449 .map_fd_put_ptr = prog_fd_array_put_ptr,
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700450};
451
452static struct bpf_map_type_list prog_array_type __read_mostly = {
453 .ops = &prog_array_ops,
454 .type = BPF_MAP_TYPE_PROG_ARRAY,
455};
456
457static int __init register_prog_array_map(void)
458{
459 bpf_register_map_type(&prog_array_type);
460 return 0;
461}
462late_initcall(register_prog_array_map);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000463
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200464static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
465 struct file *map_file)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000466{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200467 struct bpf_event_entry *ee;
468
Daniel Borkmann858d68f2016-07-16 01:15:55 +0200469 ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200470 if (ee) {
471 ee->event = perf_file->private_data;
472 ee->perf_file = perf_file;
473 ee->map_file = map_file;
474 }
475
476 return ee;
477}
478
479static void __bpf_event_entry_free(struct rcu_head *rcu)
480{
481 struct bpf_event_entry *ee;
482
483 ee = container_of(rcu, struct bpf_event_entry, rcu);
484 fput(ee->perf_file);
485 kfree(ee);
486}
487
488static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
489{
490 call_rcu(&ee->rcu, __bpf_event_entry_free);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000491}
492
Daniel Borkmannd056a782016-06-15 22:47:13 +0200493static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
494 struct file *map_file, int fd)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000495{
Kaixu Xiaea317b22015-08-06 07:02:34 +0000496 const struct perf_event_attr *attr;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200497 struct bpf_event_entry *ee;
498 struct perf_event *event;
499 struct file *perf_file;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000500
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200501 perf_file = perf_event_get(fd);
502 if (IS_ERR(perf_file))
503 return perf_file;
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -0800504
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200505 event = perf_file->private_data;
506 ee = ERR_PTR(-EINVAL);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000507
508 attr = perf_event_attrs(event);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200509 if (IS_ERR(attr) || attr->inherit)
510 goto err_out;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000511
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200512 switch (attr->type) {
513 case PERF_TYPE_SOFTWARE:
514 if (attr->config != PERF_COUNT_SW_BPF_OUTPUT)
515 goto err_out;
516 /* fall-through */
517 case PERF_TYPE_RAW:
518 case PERF_TYPE_HARDWARE:
519 ee = bpf_event_entry_gen(perf_file, map_file);
520 if (ee)
521 return ee;
522 ee = ERR_PTR(-ENOMEM);
523 /* fall-through */
524 default:
525 break;
526 }
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700527
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200528err_out:
529 fput(perf_file);
530 return ee;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000531}
532
533static void perf_event_fd_array_put_ptr(void *ptr)
534{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200535 bpf_event_entry_free_rcu(ptr);
536}
537
538static void perf_event_fd_array_release(struct bpf_map *map,
539 struct file *map_file)
540{
541 struct bpf_array *array = container_of(map, struct bpf_array, map);
542 struct bpf_event_entry *ee;
543 int i;
544
545 rcu_read_lock();
546 for (i = 0; i < array->map.max_entries; i++) {
547 ee = READ_ONCE(array->ptrs[i]);
548 if (ee && ee->map_file == map_file)
549 fd_array_map_delete_elem(map, &i);
550 }
551 rcu_read_unlock();
Kaixu Xiaea317b22015-08-06 07:02:34 +0000552}
553
554static const struct bpf_map_ops perf_event_array_ops = {
555 .map_alloc = fd_array_map_alloc,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200556 .map_free = fd_array_map_free,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000557 .map_get_next_key = array_map_get_next_key,
558 .map_lookup_elem = fd_array_map_lookup_elem,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000559 .map_delete_elem = fd_array_map_delete_elem,
560 .map_fd_get_ptr = perf_event_fd_array_get_ptr,
561 .map_fd_put_ptr = perf_event_fd_array_put_ptr,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200562 .map_release = perf_event_fd_array_release,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000563};
564
565static struct bpf_map_type_list perf_event_array_type __read_mostly = {
566 .ops = &perf_event_array_ops,
567 .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
568};
569
570static int __init register_perf_event_array_map(void)
571{
572 bpf_register_map_type(&perf_event_array_type);
573 return 0;
574}
575late_initcall(register_perf_event_array_map);
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700576
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700577#ifdef CONFIG_CGROUPS
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700578static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
579 struct file *map_file /* not used */,
580 int fd)
581{
582 return cgroup_get_from_fd(fd);
583}
584
585static void cgroup_fd_array_put_ptr(void *ptr)
586{
587 /* cgroup_put free cgrp after a rcu grace period */
588 cgroup_put(ptr);
589}
590
591static void cgroup_fd_array_free(struct bpf_map *map)
592{
593 bpf_fd_array_map_clear(map);
594 fd_array_map_free(map);
595}
596
597static const struct bpf_map_ops cgroup_array_ops = {
598 .map_alloc = fd_array_map_alloc,
599 .map_free = cgroup_fd_array_free,
600 .map_get_next_key = array_map_get_next_key,
601 .map_lookup_elem = fd_array_map_lookup_elem,
602 .map_delete_elem = fd_array_map_delete_elem,
603 .map_fd_get_ptr = cgroup_fd_array_get_ptr,
604 .map_fd_put_ptr = cgroup_fd_array_put_ptr,
605};
606
607static struct bpf_map_type_list cgroup_array_type __read_mostly = {
608 .ops = &cgroup_array_ops,
609 .type = BPF_MAP_TYPE_CGROUP_ARRAY,
610};
611
612static int __init register_cgroup_array_map(void)
613{
614 bpf_register_map_type(&cgroup_array_type);
615 return 0;
616}
617late_initcall(register_cgroup_array_map);
618#endif