Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 1 | /* 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 Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 17 | #include <linux/filter.h> |
Daniel Borkmann | 0cdf5640 | 2015-10-02 18:42:00 +0200 | [diff] [blame] | 18 | #include <linux/perf_event.h> |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 19 | |
| 20 | /* Called from syscall */ |
| 21 | static struct bpf_map *array_map_alloc(union bpf_attr *attr) |
| 22 | { |
| 23 | struct bpf_array *array; |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 24 | u32 elem_size, array_size; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 25 | |
| 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 | |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame^] | 31 | if (attr->value_size >= 1 << (KMALLOC_SHIFT_MAX - 1)) |
| 32 | /* if value_size is bigger, the user space won't be able to |
| 33 | * access the elements. |
| 34 | */ |
| 35 | return ERR_PTR(-E2BIG); |
| 36 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 37 | elem_size = round_up(attr->value_size, 8); |
| 38 | |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 39 | /* check round_up into zero and u32 overflow */ |
| 40 | if (elem_size == 0 || |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame^] | 41 | attr->max_entries > (U32_MAX - PAGE_SIZE - sizeof(*array)) / elem_size) |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 42 | return ERR_PTR(-ENOMEM); |
| 43 | |
| 44 | array_size = sizeof(*array) + attr->max_entries * elem_size; |
| 45 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 46 | /* allocate all map elements and zero-initialize them */ |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 47 | array = kzalloc(array_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 48 | if (!array) { |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 49 | array = vzalloc(array_size); |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 50 | if (!array) |
| 51 | return ERR_PTR(-ENOMEM); |
| 52 | } |
| 53 | |
| 54 | /* copy mandatory map attributes */ |
| 55 | array->map.key_size = attr->key_size; |
| 56 | array->map.value_size = attr->value_size; |
| 57 | array->map.max_entries = attr->max_entries; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 58 | array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 59 | array->elem_size = elem_size; |
| 60 | |
| 61 | return &array->map; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* Called from syscall or from eBPF program */ |
| 65 | static void *array_map_lookup_elem(struct bpf_map *map, void *key) |
| 66 | { |
| 67 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 68 | u32 index = *(u32 *)key; |
| 69 | |
| 70 | if (index >= array->map.max_entries) |
| 71 | return NULL; |
| 72 | |
| 73 | return array->value + array->elem_size * index; |
| 74 | } |
| 75 | |
| 76 | /* Called from syscall */ |
| 77 | static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key) |
| 78 | { |
| 79 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 80 | u32 index = *(u32 *)key; |
| 81 | u32 *next = (u32 *)next_key; |
| 82 | |
| 83 | if (index >= array->map.max_entries) { |
| 84 | *next = 0; |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | if (index == array->map.max_entries - 1) |
| 89 | return -ENOENT; |
| 90 | |
| 91 | *next = index + 1; |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /* Called from syscall or from eBPF program */ |
| 96 | static int array_map_update_elem(struct bpf_map *map, void *key, void *value, |
| 97 | u64 map_flags) |
| 98 | { |
| 99 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 100 | u32 index = *(u32 *)key; |
| 101 | |
| 102 | if (map_flags > BPF_EXIST) |
| 103 | /* unknown flags */ |
| 104 | return -EINVAL; |
| 105 | |
| 106 | if (index >= array->map.max_entries) |
| 107 | /* all elements were pre-allocated, cannot insert a new one */ |
| 108 | return -E2BIG; |
| 109 | |
| 110 | if (map_flags == BPF_NOEXIST) |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 111 | /* all elements already exist */ |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 112 | return -EEXIST; |
| 113 | |
Daniel Borkmann | fbca9d2 | 2015-11-30 13:02:56 +0100 | [diff] [blame] | 114 | memcpy(array->value + array->elem_size * index, value, map->value_size); |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | /* Called from syscall or from eBPF program */ |
| 119 | static int array_map_delete_elem(struct bpf_map *map, void *key) |
| 120 | { |
| 121 | return -EINVAL; |
| 122 | } |
| 123 | |
| 124 | /* Called when map->refcnt goes to zero, either from workqueue or from syscall */ |
| 125 | static void array_map_free(struct bpf_map *map) |
| 126 | { |
| 127 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 128 | |
| 129 | /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0, |
| 130 | * so the programs (can be more than one that used this map) were |
| 131 | * disconnected from events. Wait for outstanding programs to complete |
| 132 | * and free the array |
| 133 | */ |
| 134 | synchronize_rcu(); |
| 135 | |
| 136 | kvfree(array); |
| 137 | } |
| 138 | |
Daniel Borkmann | a2c83ff | 2015-03-01 12:31:42 +0100 | [diff] [blame] | 139 | static const struct bpf_map_ops array_ops = { |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 140 | .map_alloc = array_map_alloc, |
| 141 | .map_free = array_map_free, |
| 142 | .map_get_next_key = array_map_get_next_key, |
| 143 | .map_lookup_elem = array_map_lookup_elem, |
| 144 | .map_update_elem = array_map_update_elem, |
| 145 | .map_delete_elem = array_map_delete_elem, |
| 146 | }; |
| 147 | |
Daniel Borkmann | a2c83ff | 2015-03-01 12:31:42 +0100 | [diff] [blame] | 148 | static struct bpf_map_type_list array_type __read_mostly = { |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 149 | .ops = &array_ops, |
| 150 | .type = BPF_MAP_TYPE_ARRAY, |
| 151 | }; |
| 152 | |
| 153 | static int __init register_array_map(void) |
| 154 | { |
Daniel Borkmann | a2c83ff | 2015-03-01 12:31:42 +0100 | [diff] [blame] | 155 | bpf_register_map_type(&array_type); |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | late_initcall(register_array_map); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 159 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 160 | static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 161 | { |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 162 | /* only file descriptors can be stored in this type of map */ |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 163 | if (attr->value_size != sizeof(u32)) |
| 164 | return ERR_PTR(-EINVAL); |
| 165 | return array_map_alloc(attr); |
| 166 | } |
| 167 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 168 | static void fd_array_map_free(struct bpf_map *map) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 169 | { |
| 170 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 171 | int i; |
| 172 | |
| 173 | synchronize_rcu(); |
| 174 | |
| 175 | /* make sure it's empty */ |
| 176 | for (i = 0; i < array->map.max_entries; i++) |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 177 | BUG_ON(array->ptrs[i] != NULL); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 178 | kvfree(array); |
| 179 | } |
| 180 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 181 | static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 182 | { |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | /* only called from syscall */ |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 187 | static int fd_array_map_update_elem(struct bpf_map *map, void *key, |
| 188 | void *value, u64 map_flags) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 189 | { |
| 190 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 191 | void *new_ptr, *old_ptr; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 192 | u32 index = *(u32 *)key, ufd; |
| 193 | |
| 194 | if (map_flags != BPF_ANY) |
| 195 | return -EINVAL; |
| 196 | |
| 197 | if (index >= array->map.max_entries) |
| 198 | return -E2BIG; |
| 199 | |
| 200 | ufd = *(u32 *)value; |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 201 | new_ptr = map->ops->map_fd_get_ptr(map, ufd); |
| 202 | if (IS_ERR(new_ptr)) |
| 203 | return PTR_ERR(new_ptr); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 204 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 205 | old_ptr = xchg(array->ptrs + index, new_ptr); |
| 206 | if (old_ptr) |
| 207 | map->ops->map_fd_put_ptr(old_ptr); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 212 | static int fd_array_map_delete_elem(struct bpf_map *map, void *key) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 213 | { |
| 214 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 215 | void *old_ptr; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 216 | u32 index = *(u32 *)key; |
| 217 | |
| 218 | if (index >= array->map.max_entries) |
| 219 | return -E2BIG; |
| 220 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 221 | old_ptr = xchg(array->ptrs + index, NULL); |
| 222 | if (old_ptr) { |
| 223 | map->ops->map_fd_put_ptr(old_ptr); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | } else { |
| 226 | return -ENOENT; |
| 227 | } |
| 228 | } |
| 229 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 230 | static void *prog_fd_array_get_ptr(struct bpf_map *map, int fd) |
| 231 | { |
| 232 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 233 | struct bpf_prog *prog = bpf_prog_get(fd); |
| 234 | if (IS_ERR(prog)) |
| 235 | return prog; |
| 236 | |
| 237 | if (!bpf_prog_array_compatible(array, prog)) { |
| 238 | bpf_prog_put(prog); |
| 239 | return ERR_PTR(-EINVAL); |
| 240 | } |
| 241 | return prog; |
| 242 | } |
| 243 | |
| 244 | static void prog_fd_array_put_ptr(void *ptr) |
| 245 | { |
| 246 | struct bpf_prog *prog = ptr; |
| 247 | |
| 248 | bpf_prog_put_rcu(prog); |
| 249 | } |
| 250 | |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 251 | /* decrement refcnt of all bpf_progs that are stored in this map */ |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 252 | void bpf_fd_array_map_clear(struct bpf_map *map) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 253 | { |
| 254 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 255 | int i; |
| 256 | |
| 257 | for (i = 0; i < array->map.max_entries; i++) |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 258 | fd_array_map_delete_elem(map, &i); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static const struct bpf_map_ops prog_array_ops = { |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 262 | .map_alloc = fd_array_map_alloc, |
| 263 | .map_free = fd_array_map_free, |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 264 | .map_get_next_key = array_map_get_next_key, |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 265 | .map_lookup_elem = fd_array_map_lookup_elem, |
| 266 | .map_update_elem = fd_array_map_update_elem, |
| 267 | .map_delete_elem = fd_array_map_delete_elem, |
| 268 | .map_fd_get_ptr = prog_fd_array_get_ptr, |
| 269 | .map_fd_put_ptr = prog_fd_array_put_ptr, |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | static struct bpf_map_type_list prog_array_type __read_mostly = { |
| 273 | .ops = &prog_array_ops, |
| 274 | .type = BPF_MAP_TYPE_PROG_ARRAY, |
| 275 | }; |
| 276 | |
| 277 | static int __init register_prog_array_map(void) |
| 278 | { |
| 279 | bpf_register_map_type(&prog_array_type); |
| 280 | return 0; |
| 281 | } |
| 282 | late_initcall(register_prog_array_map); |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 283 | |
| 284 | static void perf_event_array_map_free(struct bpf_map *map) |
| 285 | { |
| 286 | bpf_fd_array_map_clear(map); |
| 287 | fd_array_map_free(map); |
| 288 | } |
| 289 | |
| 290 | static void *perf_event_fd_array_get_ptr(struct bpf_map *map, int fd) |
| 291 | { |
| 292 | struct perf_event *event; |
| 293 | const struct perf_event_attr *attr; |
| 294 | |
| 295 | event = perf_event_get(fd); |
| 296 | if (IS_ERR(event)) |
| 297 | return event; |
| 298 | |
| 299 | attr = perf_event_attrs(event); |
| 300 | if (IS_ERR(attr)) |
Alexei Starovoitov | 62544ce | 2015-10-22 17:10:14 -0700 | [diff] [blame] | 301 | goto err; |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 302 | |
Alexei Starovoitov | 62544ce | 2015-10-22 17:10:14 -0700 | [diff] [blame] | 303 | if (attr->inherit) |
| 304 | goto err; |
| 305 | |
| 306 | if (attr->type == PERF_TYPE_RAW) |
| 307 | return event; |
| 308 | |
| 309 | if (attr->type == PERF_TYPE_HARDWARE) |
| 310 | return event; |
| 311 | |
| 312 | if (attr->type == PERF_TYPE_SOFTWARE && |
| 313 | attr->config == PERF_COUNT_SW_BPF_OUTPUT) |
| 314 | return event; |
| 315 | err: |
| 316 | perf_event_release_kernel(event); |
| 317 | return ERR_PTR(-EINVAL); |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static void perf_event_fd_array_put_ptr(void *ptr) |
| 321 | { |
| 322 | struct perf_event *event = ptr; |
| 323 | |
| 324 | perf_event_release_kernel(event); |
| 325 | } |
| 326 | |
| 327 | static const struct bpf_map_ops perf_event_array_ops = { |
| 328 | .map_alloc = fd_array_map_alloc, |
| 329 | .map_free = perf_event_array_map_free, |
| 330 | .map_get_next_key = array_map_get_next_key, |
| 331 | .map_lookup_elem = fd_array_map_lookup_elem, |
| 332 | .map_update_elem = fd_array_map_update_elem, |
| 333 | .map_delete_elem = fd_array_map_delete_elem, |
| 334 | .map_fd_get_ptr = perf_event_fd_array_get_ptr, |
| 335 | .map_fd_put_ptr = perf_event_fd_array_put_ptr, |
| 336 | }; |
| 337 | |
| 338 | static struct bpf_map_type_list perf_event_array_type __read_mostly = { |
| 339 | .ops = &perf_event_array_ops, |
| 340 | .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY, |
| 341 | }; |
| 342 | |
| 343 | static int __init register_perf_event_array_map(void) |
| 344 | { |
| 345 | bpf_register_map_type(&perf_event_array_type); |
| 346 | return 0; |
| 347 | } |
| 348 | late_initcall(register_perf_event_array_map); |