blob: 96e9c5c1dfc9de18234c63b24232ce89d344ddd6 [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;
Martin KaFai Lau96eabe72017-08-18 11:28:00 -070052 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080053 struct bpf_array *array;
Alexei Starovoitova10423b2016-02-01 22:39:54 -080054 u64 array_size;
55 u32 elem_size;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080056
57 /* check sanity of attributes */
58 if (attr->max_entries == 0 || attr->key_size != 4 ||
Martin KaFai Lau96eabe72017-08-18 11:28:00 -070059 attr->value_size == 0 || attr->map_flags & ~BPF_F_NUMA_NODE ||
60 (percpu && numa_node != NUMA_NO_NODE))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080061 return ERR_PTR(-EINVAL);
62
Michal Hocko7984c272017-01-10 16:57:30 -080063 if (attr->value_size > KMALLOC_MAX_SIZE)
Alexei Starovoitov01b3f522015-11-29 16:59:35 -080064 /* if value_size is bigger, the user space won't be able to
65 * access the elements.
66 */
67 return ERR_PTR(-E2BIG);
68
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080069 elem_size = round_up(attr->value_size, 8);
70
Alexei Starovoitova10423b2016-02-01 22:39:54 -080071 array_size = sizeof(*array);
72 if (percpu)
73 array_size += (u64) attr->max_entries * sizeof(void *);
74 else
75 array_size += (u64) attr->max_entries * elem_size;
76
77 /* make sure there is no u32 overflow later in round_up() */
78 if (array_size >= U32_MAX - PAGE_SIZE)
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -080079 return ERR_PTR(-ENOMEM);
80
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080081 /* allocate all map elements and zero-initialize them */
Martin KaFai Lau96eabe72017-08-18 11:28:00 -070082 array = bpf_map_area_alloc(array_size, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +010083 if (!array)
84 return ERR_PTR(-ENOMEM);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080085
86 /* copy mandatory map attributes */
Alexei Starovoitova10423b2016-02-01 22:39:54 -080087 array->map.map_type = attr->map_type;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080088 array->map.key_size = attr->key_size;
89 array->map.value_size = attr->value_size;
90 array->map.max_entries = attr->max_entries;
Daniel Borkmanna3163382017-05-25 01:05:08 +020091 array->map.map_flags = attr->map_flags;
Martin KaFai Lau96eabe72017-08-18 11:28:00 -070092 array->map.numa_node = numa_node;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080093 array->elem_size = elem_size;
94
Alexei Starovoitova10423b2016-02-01 22:39:54 -080095 if (!percpu)
96 goto out;
97
98 array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
99
100 if (array_size >= U32_MAX - PAGE_SIZE ||
101 elem_size > PCPU_MIN_UNIT_SIZE || bpf_array_alloc_percpu(array)) {
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100102 bpf_map_area_free(array);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800103 return ERR_PTR(-ENOMEM);
104 }
105out:
106 array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
107
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800108 return &array->map;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800109}
110
111/* Called from syscall or from eBPF program */
112static void *array_map_lookup_elem(struct bpf_map *map, void *key)
113{
114 struct bpf_array *array = container_of(map, struct bpf_array, map);
115 u32 index = *(u32 *)key;
116
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800117 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800118 return NULL;
119
120 return array->value + array->elem_size * index;
121}
122
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700123/* emit BPF instructions equivalent to C code of array_map_lookup_elem() */
124static u32 array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
125{
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700126 struct bpf_insn *insn = insn_buf;
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700127 u32 elem_size = round_up(map->value_size, 8);
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700128 const int ret = BPF_REG_0;
129 const int map_ptr = BPF_REG_1;
130 const int index = BPF_REG_2;
131
132 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
133 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700134 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3);
135
136 if (is_power_of_2(elem_size)) {
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700137 *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
138 } else {
139 *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
140 }
141 *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
142 *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
143 *insn++ = BPF_MOV64_IMM(ret, 0);
144 return insn - insn_buf;
145}
146
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800147/* Called from eBPF program */
148static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
149{
150 struct bpf_array *array = container_of(map, struct bpf_array, map);
151 u32 index = *(u32 *)key;
152
153 if (unlikely(index >= array->map.max_entries))
154 return NULL;
155
156 return this_cpu_ptr(array->pptrs[index]);
157}
158
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800159int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
160{
161 struct bpf_array *array = container_of(map, struct bpf_array, map);
162 u32 index = *(u32 *)key;
163 void __percpu *pptr;
164 int cpu, off = 0;
165 u32 size;
166
167 if (unlikely(index >= array->map.max_entries))
168 return -ENOENT;
169
170 /* per_cpu areas are zero-filled and bpf programs can only
171 * access 'value_size' of them, so copying rounded areas
172 * will not leak any kernel data
173 */
174 size = round_up(map->value_size, 8);
175 rcu_read_lock();
176 pptr = array->pptrs[index];
177 for_each_possible_cpu(cpu) {
178 bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
179 off += size;
180 }
181 rcu_read_unlock();
182 return 0;
183}
184
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800185/* Called from syscall */
186static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
187{
188 struct bpf_array *array = container_of(map, struct bpf_array, map);
Teng Qin8fe45922017-04-24 19:00:37 -0700189 u32 index = key ? *(u32 *)key : U32_MAX;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800190 u32 *next = (u32 *)next_key;
191
192 if (index >= array->map.max_entries) {
193 *next = 0;
194 return 0;
195 }
196
197 if (index == array->map.max_entries - 1)
198 return -ENOENT;
199
200 *next = index + 1;
201 return 0;
202}
203
204/* Called from syscall or from eBPF program */
205static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
206 u64 map_flags)
207{
208 struct bpf_array *array = container_of(map, struct bpf_array, map);
209 u32 index = *(u32 *)key;
210
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800211 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800212 /* unknown flags */
213 return -EINVAL;
214
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800215 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800216 /* all elements were pre-allocated, cannot insert a new one */
217 return -E2BIG;
218
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800219 if (unlikely(map_flags == BPF_NOEXIST))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800220 /* all elements already exist */
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800221 return -EEXIST;
222
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800223 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
224 memcpy(this_cpu_ptr(array->pptrs[index]),
225 value, map->value_size);
226 else
227 memcpy(array->value + array->elem_size * index,
228 value, map->value_size);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800229 return 0;
230}
231
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800232int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
233 u64 map_flags)
234{
235 struct bpf_array *array = container_of(map, struct bpf_array, map);
236 u32 index = *(u32 *)key;
237 void __percpu *pptr;
238 int cpu, off = 0;
239 u32 size;
240
241 if (unlikely(map_flags > BPF_EXIST))
242 /* unknown flags */
243 return -EINVAL;
244
245 if (unlikely(index >= array->map.max_entries))
246 /* all elements were pre-allocated, cannot insert a new one */
247 return -E2BIG;
248
249 if (unlikely(map_flags == BPF_NOEXIST))
250 /* all elements already exist */
251 return -EEXIST;
252
253 /* the user space will provide round_up(value_size, 8) bytes that
254 * will be copied into per-cpu area. bpf programs can only access
255 * value_size of it. During lookup the same extra bytes will be
256 * returned or zeros which were zero-filled by percpu_alloc,
257 * so no kernel data leaks possible
258 */
259 size = round_up(map->value_size, 8);
260 rcu_read_lock();
261 pptr = array->pptrs[index];
262 for_each_possible_cpu(cpu) {
263 bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
264 off += size;
265 }
266 rcu_read_unlock();
267 return 0;
268}
269
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800270/* Called from syscall or from eBPF program */
271static int array_map_delete_elem(struct bpf_map *map, void *key)
272{
273 return -EINVAL;
274}
275
276/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
277static void array_map_free(struct bpf_map *map)
278{
279 struct bpf_array *array = container_of(map, struct bpf_array, map);
280
281 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
282 * so the programs (can be more than one that used this map) were
283 * disconnected from events. Wait for outstanding programs to complete
284 * and free the array
285 */
286 synchronize_rcu();
287
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800288 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
289 bpf_array_free_percpu(array);
290
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100291 bpf_map_area_free(array);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800292}
293
Johannes Berg40077e02017-04-11 15:34:58 +0200294const struct bpf_map_ops array_map_ops = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800295 .map_alloc = array_map_alloc,
296 .map_free = array_map_free,
297 .map_get_next_key = array_map_get_next_key,
298 .map_lookup_elem = array_map_lookup_elem,
299 .map_update_elem = array_map_update_elem,
300 .map_delete_elem = array_map_delete_elem,
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700301 .map_gen_lookup = array_map_gen_lookup,
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800302};
303
Johannes Berg40077e02017-04-11 15:34:58 +0200304const struct bpf_map_ops percpu_array_map_ops = {
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800305 .map_alloc = array_map_alloc,
306 .map_free = array_map_free,
307 .map_get_next_key = array_map_get_next_key,
308 .map_lookup_elem = percpu_array_map_lookup_elem,
309 .map_update_elem = array_map_update_elem,
310 .map_delete_elem = array_map_delete_elem,
311};
312
Wang Nan2a36f0b2015-08-06 07:02:33 +0000313static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700314{
Wang Nan2a36f0b2015-08-06 07:02:33 +0000315 /* only file descriptors can be stored in this type of map */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700316 if (attr->value_size != sizeof(u32))
317 return ERR_PTR(-EINVAL);
318 return array_map_alloc(attr);
319}
320
Wang Nan2a36f0b2015-08-06 07:02:33 +0000321static void fd_array_map_free(struct bpf_map *map)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700322{
323 struct bpf_array *array = container_of(map, struct bpf_array, map);
324 int i;
325
326 synchronize_rcu();
327
328 /* make sure it's empty */
329 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000330 BUG_ON(array->ptrs[i] != NULL);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100331
332 bpf_map_area_free(array);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700333}
334
Wang Nan2a36f0b2015-08-06 07:02:33 +0000335static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700336{
337 return NULL;
338}
339
340/* only called from syscall */
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700341int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
342{
343 void **elem, *ptr;
344 int ret = 0;
345
346 if (!map->ops->map_fd_sys_lookup_elem)
347 return -ENOTSUPP;
348
349 rcu_read_lock();
350 elem = array_map_lookup_elem(map, key);
351 if (elem && (ptr = READ_ONCE(*elem)))
352 *value = map->ops->map_fd_sys_lookup_elem(ptr);
353 else
354 ret = -ENOENT;
355 rcu_read_unlock();
356
357 return ret;
358}
359
360/* only called from syscall */
Daniel Borkmannd056a782016-06-15 22:47:13 +0200361int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
362 void *key, void *value, u64 map_flags)
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 *new_ptr, *old_ptr;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700366 u32 index = *(u32 *)key, ufd;
367
368 if (map_flags != BPF_ANY)
369 return -EINVAL;
370
371 if (index >= array->map.max_entries)
372 return -E2BIG;
373
374 ufd = *(u32 *)value;
Daniel Borkmannd056a782016-06-15 22:47:13 +0200375 new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000376 if (IS_ERR(new_ptr))
377 return PTR_ERR(new_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700378
Wang Nan2a36f0b2015-08-06 07:02:33 +0000379 old_ptr = xchg(array->ptrs + index, new_ptr);
380 if (old_ptr)
381 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700382
383 return 0;
384}
385
Wang Nan2a36f0b2015-08-06 07:02:33 +0000386static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700387{
388 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000389 void *old_ptr;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700390 u32 index = *(u32 *)key;
391
392 if (index >= array->map.max_entries)
393 return -E2BIG;
394
Wang Nan2a36f0b2015-08-06 07:02:33 +0000395 old_ptr = xchg(array->ptrs + index, NULL);
396 if (old_ptr) {
397 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700398 return 0;
399 } else {
400 return -ENOENT;
401 }
402}
403
Daniel Borkmannd056a782016-06-15 22:47:13 +0200404static void *prog_fd_array_get_ptr(struct bpf_map *map,
405 struct file *map_file, int fd)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000406{
407 struct bpf_array *array = container_of(map, struct bpf_array, map);
408 struct bpf_prog *prog = bpf_prog_get(fd);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200409
Wang Nan2a36f0b2015-08-06 07:02:33 +0000410 if (IS_ERR(prog))
411 return prog;
412
413 if (!bpf_prog_array_compatible(array, prog)) {
414 bpf_prog_put(prog);
415 return ERR_PTR(-EINVAL);
416 }
Daniel Borkmannd056a782016-06-15 22:47:13 +0200417
Wang Nan2a36f0b2015-08-06 07:02:33 +0000418 return prog;
419}
420
421static void prog_fd_array_put_ptr(void *ptr)
422{
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200423 bpf_prog_put(ptr);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000424}
425
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700426static u32 prog_fd_array_sys_lookup_elem(void *ptr)
427{
428 return ((struct bpf_prog *)ptr)->aux->id;
429}
430
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700431/* decrement refcnt of all bpf_progs that are stored in this map */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000432void bpf_fd_array_map_clear(struct bpf_map *map)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700433{
434 struct bpf_array *array = container_of(map, struct bpf_array, map);
435 int i;
436
437 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000438 fd_array_map_delete_elem(map, &i);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700439}
440
Johannes Berg40077e02017-04-11 15:34:58 +0200441const struct bpf_map_ops prog_array_map_ops = {
Wang Nan2a36f0b2015-08-06 07:02:33 +0000442 .map_alloc = fd_array_map_alloc,
443 .map_free = fd_array_map_free,
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700444 .map_get_next_key = array_map_get_next_key,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000445 .map_lookup_elem = fd_array_map_lookup_elem,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000446 .map_delete_elem = fd_array_map_delete_elem,
447 .map_fd_get_ptr = prog_fd_array_get_ptr,
448 .map_fd_put_ptr = prog_fd_array_put_ptr,
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700449 .map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700450};
451
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200452static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
453 struct file *map_file)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000454{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200455 struct bpf_event_entry *ee;
456
Daniel Borkmann858d68f2016-07-16 01:15:55 +0200457 ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200458 if (ee) {
459 ee->event = perf_file->private_data;
460 ee->perf_file = perf_file;
461 ee->map_file = map_file;
462 }
463
464 return ee;
465}
466
467static void __bpf_event_entry_free(struct rcu_head *rcu)
468{
469 struct bpf_event_entry *ee;
470
471 ee = container_of(rcu, struct bpf_event_entry, rcu);
472 fput(ee->perf_file);
473 kfree(ee);
474}
475
476static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
477{
478 call_rcu(&ee->rcu, __bpf_event_entry_free);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000479}
480
Daniel Borkmannd056a782016-06-15 22:47:13 +0200481static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
482 struct file *map_file, int fd)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000483{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200484 struct bpf_event_entry *ee;
485 struct perf_event *event;
486 struct file *perf_file;
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700487 u64 value;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000488
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200489 perf_file = perf_event_get(fd);
490 if (IS_ERR(perf_file))
491 return perf_file;
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -0800492
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700493 ee = ERR_PTR(-EOPNOTSUPP);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200494 event = perf_file->private_data;
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700495 if (perf_event_read_local(event, &value) == -EOPNOTSUPP)
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200496 goto err_out;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000497
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700498 ee = bpf_event_entry_gen(perf_file, map_file);
499 if (ee)
500 return ee;
501 ee = ERR_PTR(-ENOMEM);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200502err_out:
503 fput(perf_file);
504 return ee;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000505}
506
507static void perf_event_fd_array_put_ptr(void *ptr)
508{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200509 bpf_event_entry_free_rcu(ptr);
510}
511
512static void perf_event_fd_array_release(struct bpf_map *map,
513 struct file *map_file)
514{
515 struct bpf_array *array = container_of(map, struct bpf_array, map);
516 struct bpf_event_entry *ee;
517 int i;
518
519 rcu_read_lock();
520 for (i = 0; i < array->map.max_entries; i++) {
521 ee = READ_ONCE(array->ptrs[i]);
522 if (ee && ee->map_file == map_file)
523 fd_array_map_delete_elem(map, &i);
524 }
525 rcu_read_unlock();
Kaixu Xiaea317b22015-08-06 07:02:34 +0000526}
527
Johannes Berg40077e02017-04-11 15:34:58 +0200528const struct bpf_map_ops perf_event_array_map_ops = {
Kaixu Xiaea317b22015-08-06 07:02:34 +0000529 .map_alloc = fd_array_map_alloc,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200530 .map_free = fd_array_map_free,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000531 .map_get_next_key = array_map_get_next_key,
532 .map_lookup_elem = fd_array_map_lookup_elem,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000533 .map_delete_elem = fd_array_map_delete_elem,
534 .map_fd_get_ptr = perf_event_fd_array_get_ptr,
535 .map_fd_put_ptr = perf_event_fd_array_put_ptr,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200536 .map_release = perf_event_fd_array_release,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000537};
538
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700539#ifdef CONFIG_CGROUPS
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700540static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
541 struct file *map_file /* not used */,
542 int fd)
543{
544 return cgroup_get_from_fd(fd);
545}
546
547static void cgroup_fd_array_put_ptr(void *ptr)
548{
549 /* cgroup_put free cgrp after a rcu grace period */
550 cgroup_put(ptr);
551}
552
553static void cgroup_fd_array_free(struct bpf_map *map)
554{
555 bpf_fd_array_map_clear(map);
556 fd_array_map_free(map);
557}
558
Johannes Berg40077e02017-04-11 15:34:58 +0200559const struct bpf_map_ops cgroup_array_map_ops = {
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700560 .map_alloc = fd_array_map_alloc,
561 .map_free = cgroup_fd_array_free,
562 .map_get_next_key = array_map_get_next_key,
563 .map_lookup_elem = fd_array_map_lookup_elem,
564 .map_delete_elem = fd_array_map_delete_elem,
565 .map_fd_get_ptr = cgroup_fd_array_get_ptr,
566 .map_fd_put_ptr = cgroup_fd_array_put_ptr,
567};
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700568#endif
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700569
570static struct bpf_map *array_of_map_alloc(union bpf_attr *attr)
571{
572 struct bpf_map *map, *inner_map_meta;
573
574 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
575 if (IS_ERR(inner_map_meta))
576 return inner_map_meta;
577
578 map = fd_array_map_alloc(attr);
579 if (IS_ERR(map)) {
580 bpf_map_meta_free(inner_map_meta);
581 return map;
582 }
583
584 map->inner_map_meta = inner_map_meta;
585
586 return map;
587}
588
589static void array_of_map_free(struct bpf_map *map)
590{
591 /* map->inner_map_meta is only accessed by syscall which
592 * is protected by fdget/fdput.
593 */
594 bpf_map_meta_free(map->inner_map_meta);
595 bpf_fd_array_map_clear(map);
596 fd_array_map_free(map);
597}
598
599static void *array_of_map_lookup_elem(struct bpf_map *map, void *key)
600{
601 struct bpf_map **inner_map = array_map_lookup_elem(map, key);
602
603 if (!inner_map)
604 return NULL;
605
606 return READ_ONCE(*inner_map);
607}
608
Johannes Berg40077e02017-04-11 15:34:58 +0200609const struct bpf_map_ops array_of_maps_map_ops = {
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700610 .map_alloc = array_of_map_alloc,
611 .map_free = array_of_map_free,
612 .map_get_next_key = array_map_get_next_key,
613 .map_lookup_elem = array_of_map_lookup_elem,
614 .map_delete_elem = fd_array_map_delete_elem,
615 .map_fd_get_ptr = bpf_map_fd_get_ptr,
616 .map_fd_put_ptr = bpf_map_fd_put_ptr,
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700617 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700618};