blob: 3f4c99e06c6bbf8a5e28f882633e8161f3974929 [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>
14#include <linux/vmalloc.h>
15#include <linux/slab.h>
16#include <linux/mm.h>
Alexei Starovoitov04fd61a2015-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
20/* Called from syscall */
21static struct bpf_map *array_map_alloc(union bpf_attr *attr)
22{
23 struct bpf_array *array;
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -080024 u32 elem_size, array_size;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080025
26 /* check sanity of attributes */
27 if (attr->max_entries == 0 || attr->key_size != 4 ||
28 attr->value_size == 0)
29 return ERR_PTR(-EINVAL);
30
31 elem_size = round_up(attr->value_size, 8);
32
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -080033 /* check round_up into zero and u32 overflow */
34 if (elem_size == 0 ||
35 attr->max_entries > (U32_MAX - sizeof(*array)) / elem_size)
36 return ERR_PTR(-ENOMEM);
37
38 array_size = sizeof(*array) + attr->max_entries * elem_size;
39
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080040 /* allocate all map elements and zero-initialize them */
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -080041 array = kzalloc(array_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080042 if (!array) {
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -080043 array = vzalloc(array_size);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080044 if (!array)
45 return ERR_PTR(-ENOMEM);
46 }
47
48 /* copy mandatory map attributes */
49 array->map.key_size = attr->key_size;
50 array->map.value_size = attr->value_size;
51 array->map.max_entries = attr->max_entries;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070052 array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080053 array->elem_size = elem_size;
54
55 return &array->map;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080056}
57
58/* Called from syscall or from eBPF program */
59static void *array_map_lookup_elem(struct bpf_map *map, void *key)
60{
61 struct bpf_array *array = container_of(map, struct bpf_array, map);
62 u32 index = *(u32 *)key;
63
64 if (index >= array->map.max_entries)
65 return NULL;
66
67 return array->value + array->elem_size * index;
68}
69
70/* Called from syscall */
71static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
72{
73 struct bpf_array *array = container_of(map, struct bpf_array, map);
74 u32 index = *(u32 *)key;
75 u32 *next = (u32 *)next_key;
76
77 if (index >= array->map.max_entries) {
78 *next = 0;
79 return 0;
80 }
81
82 if (index == array->map.max_entries - 1)
83 return -ENOENT;
84
85 *next = index + 1;
86 return 0;
87}
88
89/* Called from syscall or from eBPF program */
90static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
91 u64 map_flags)
92{
93 struct bpf_array *array = container_of(map, struct bpf_array, map);
94 u32 index = *(u32 *)key;
95
96 if (map_flags > BPF_EXIST)
97 /* unknown flags */
98 return -EINVAL;
99
100 if (index >= array->map.max_entries)
101 /* all elements were pre-allocated, cannot insert a new one */
102 return -E2BIG;
103
104 if (map_flags == BPF_NOEXIST)
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800105 /* all elements already exist */
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800106 return -EEXIST;
107
108 memcpy(array->value + array->elem_size * index, value, array->elem_size);
109 return 0;
110}
111
112/* Called from syscall or from eBPF program */
113static int array_map_delete_elem(struct bpf_map *map, void *key)
114{
115 return -EINVAL;
116}
117
118/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
119static void array_map_free(struct bpf_map *map)
120{
121 struct bpf_array *array = container_of(map, struct bpf_array, map);
122
123 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
124 * so the programs (can be more than one that used this map) were
125 * disconnected from events. Wait for outstanding programs to complete
126 * and free the array
127 */
128 synchronize_rcu();
129
130 kvfree(array);
131}
132
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100133static const struct bpf_map_ops array_ops = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800134 .map_alloc = array_map_alloc,
135 .map_free = array_map_free,
136 .map_get_next_key = array_map_get_next_key,
137 .map_lookup_elem = array_map_lookup_elem,
138 .map_update_elem = array_map_update_elem,
139 .map_delete_elem = array_map_delete_elem,
140};
141
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100142static struct bpf_map_type_list array_type __read_mostly = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800143 .ops = &array_ops,
144 .type = BPF_MAP_TYPE_ARRAY,
145};
146
147static int __init register_array_map(void)
148{
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100149 bpf_register_map_type(&array_type);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800150 return 0;
151}
152late_initcall(register_array_map);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700153
Wang Nan2a36f0b2015-08-06 07:02:33 +0000154static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700155{
Wang Nan2a36f0b2015-08-06 07:02:33 +0000156 /* only file descriptors can be stored in this type of map */
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700157 if (attr->value_size != sizeof(u32))
158 return ERR_PTR(-EINVAL);
159 return array_map_alloc(attr);
160}
161
Wang Nan2a36f0b2015-08-06 07:02:33 +0000162static void fd_array_map_free(struct bpf_map *map)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700163{
164 struct bpf_array *array = container_of(map, struct bpf_array, map);
165 int i;
166
167 synchronize_rcu();
168
169 /* make sure it's empty */
170 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000171 BUG_ON(array->ptrs[i] != NULL);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700172 kvfree(array);
173}
174
Wang Nan2a36f0b2015-08-06 07:02:33 +0000175static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700176{
177 return NULL;
178}
179
180/* only called from syscall */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000181static int fd_array_map_update_elem(struct bpf_map *map, void *key,
182 void *value, u64 map_flags)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700183{
184 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000185 void *new_ptr, *old_ptr;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700186 u32 index = *(u32 *)key, ufd;
187
188 if (map_flags != BPF_ANY)
189 return -EINVAL;
190
191 if (index >= array->map.max_entries)
192 return -E2BIG;
193
194 ufd = *(u32 *)value;
Wang Nan2a36f0b2015-08-06 07:02:33 +0000195 new_ptr = map->ops->map_fd_get_ptr(map, ufd);
196 if (IS_ERR(new_ptr))
197 return PTR_ERR(new_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700198
Wang Nan2a36f0b2015-08-06 07:02:33 +0000199 old_ptr = xchg(array->ptrs + index, new_ptr);
200 if (old_ptr)
201 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700202
203 return 0;
204}
205
Wang Nan2a36f0b2015-08-06 07:02:33 +0000206static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700207{
208 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000209 void *old_ptr;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700210 u32 index = *(u32 *)key;
211
212 if (index >= array->map.max_entries)
213 return -E2BIG;
214
Wang Nan2a36f0b2015-08-06 07:02:33 +0000215 old_ptr = xchg(array->ptrs + index, NULL);
216 if (old_ptr) {
217 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700218 return 0;
219 } else {
220 return -ENOENT;
221 }
222}
223
Wang Nan2a36f0b2015-08-06 07:02:33 +0000224static void *prog_fd_array_get_ptr(struct bpf_map *map, int fd)
225{
226 struct bpf_array *array = container_of(map, struct bpf_array, map);
227 struct bpf_prog *prog = bpf_prog_get(fd);
228 if (IS_ERR(prog))
229 return prog;
230
231 if (!bpf_prog_array_compatible(array, prog)) {
232 bpf_prog_put(prog);
233 return ERR_PTR(-EINVAL);
234 }
235 return prog;
236}
237
238static void prog_fd_array_put_ptr(void *ptr)
239{
240 struct bpf_prog *prog = ptr;
241
242 bpf_prog_put_rcu(prog);
243}
244
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700245/* decrement refcnt of all bpf_progs that are stored in this map */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000246void bpf_fd_array_map_clear(struct bpf_map *map)
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700247{
248 struct bpf_array *array = container_of(map, struct bpf_array, map);
249 int i;
250
251 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000252 fd_array_map_delete_elem(map, &i);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700253}
254
255static const struct bpf_map_ops prog_array_ops = {
Wang Nan2a36f0b2015-08-06 07:02:33 +0000256 .map_alloc = fd_array_map_alloc,
257 .map_free = fd_array_map_free,
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700258 .map_get_next_key = array_map_get_next_key,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000259 .map_lookup_elem = fd_array_map_lookup_elem,
260 .map_update_elem = fd_array_map_update_elem,
261 .map_delete_elem = fd_array_map_delete_elem,
262 .map_fd_get_ptr = prog_fd_array_get_ptr,
263 .map_fd_put_ptr = prog_fd_array_put_ptr,
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700264};
265
266static struct bpf_map_type_list prog_array_type __read_mostly = {
267 .ops = &prog_array_ops,
268 .type = BPF_MAP_TYPE_PROG_ARRAY,
269};
270
271static int __init register_prog_array_map(void)
272{
273 bpf_register_map_type(&prog_array_type);
274 return 0;
275}
276late_initcall(register_prog_array_map);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000277
278static void perf_event_array_map_free(struct bpf_map *map)
279{
280 bpf_fd_array_map_clear(map);
281 fd_array_map_free(map);
282}
283
284static void *perf_event_fd_array_get_ptr(struct bpf_map *map, int fd)
285{
286 struct perf_event *event;
287 const struct perf_event_attr *attr;
288
289 event = perf_event_get(fd);
290 if (IS_ERR(event))
291 return event;
292
293 attr = perf_event_attrs(event);
294 if (IS_ERR(attr))
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700295 goto err;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000296
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700297 if (attr->inherit)
298 goto err;
299
300 if (attr->type == PERF_TYPE_RAW)
301 return event;
302
303 if (attr->type == PERF_TYPE_HARDWARE)
304 return event;
305
306 if (attr->type == PERF_TYPE_SOFTWARE &&
307 attr->config == PERF_COUNT_SW_BPF_OUTPUT)
308 return event;
309err:
310 perf_event_release_kernel(event);
311 return ERR_PTR(-EINVAL);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000312}
313
314static void perf_event_fd_array_put_ptr(void *ptr)
315{
316 struct perf_event *event = ptr;
317
318 perf_event_release_kernel(event);
319}
320
321static const struct bpf_map_ops perf_event_array_ops = {
322 .map_alloc = fd_array_map_alloc,
323 .map_free = perf_event_array_map_free,
324 .map_get_next_key = array_map_get_next_key,
325 .map_lookup_elem = fd_array_map_lookup_elem,
326 .map_update_elem = fd_array_map_update_elem,
327 .map_delete_elem = fd_array_map_delete_elem,
328 .map_fd_get_ptr = perf_event_fd_array_get_ptr,
329 .map_fd_put_ptr = perf_event_fd_array_put_ptr,
330};
331
332static struct bpf_map_type_list perf_event_array_type __read_mostly = {
333 .ops = &perf_event_array_ops,
334 .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
335};
336
337static int __init register_perf_event_array_map(void)
338{
339 bpf_register_map_type(&perf_event_array_type);
340 return 0;
341}
342late_initcall(register_perf_event_array_map);