| 1 | |
| 2 | * Copyright (c) 2016,2017 Facebook
|
| 3 | *
|
| 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>
|
| 15 | #include <linux/slab.h>
|
| 16 | #include <linux/mm.h>
|
| 17 | #include <linux/filter.h>
|
| 18 | #include <linux/perf_event.h>
|
| 19 |
|
| 20 | #include "map_in_map.h"
|
| 21 |
|
| 22 | #define ARRAY_CREATE_FLAG_MASK \
|
| 23 | (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
|
| 24 |
|
| 25 | static void bpf_array_free_percpu(struct bpf_array *array)
|
| 26 | {
|
| 27 | int i;
|
| 28 |
|
| 29 | for (i = 0; i < array->map.max_entries; i++)
|
| 30 | free_percpu(array->pptrs[i]);
|
| 31 | }
|
| 32 |
|
| 33 | static int bpf_array_alloc_percpu(struct bpf_array *array)
|
| 34 | {
|
| 35 | void __percpu *ptr;
|
| 36 | int i;
|
| 37 |
|
| 38 | for (i = 0; i < array->map.max_entries; i++) {
|
| 39 | ptr = __alloc_percpu_gfp(array->elem_size, 8,
|
| 40 | GFP_USER | __GFP_NOWARN);
|
| 41 | if (!ptr) {
|
| 42 | bpf_array_free_percpu(array);
|
| 43 | return -ENOMEM;
|
| 44 | }
|
| 45 | array->pptrs[i] = ptr;
|
| 46 | }
|
| 47 |
|
| 48 | return 0;
|
| 49 | }
|
| 50 |
|
| 51 |
|
| 52 | static int array_map_alloc_check(union bpf_attr *attr)
|
| 53 | {
|
| 54 | bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
|
| 55 | int numa_node = bpf_map_attr_numa_node(attr);
|
| 56 |
|
| 57 |
|
| 58 | if (attr->max_entries == 0 || attr->key_size != 4 ||
|
| 59 | attr->value_size == 0 ||
|
| 60 | attr->map_flags & ~ARRAY_CREATE_FLAG_MASK ||
|
| 61 | (percpu && numa_node != NUMA_NO_NODE))
|
| 62 | return -EINVAL;
|
| 63 |
|
| 64 | if (attr->value_size > KMALLOC_MAX_SIZE)
|
| 65 | |
| 66 | * access the elements.
|
| 67 |
|
| 68 | return -E2BIG;
|
| 69 |
|
| 70 | return 0;
|
| 71 | }
|
| 72 |
|
| 73 | static struct bpf_map *array_map_alloc(union bpf_attr *attr)
|
| 74 | {
|
| 75 | bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
|
| 76 | int numa_node = bpf_map_attr_numa_node(attr);
|
| 77 | u32 elem_size, index_mask, max_entries;
|
| 78 | bool unpriv = !capable(CAP_SYS_ADMIN);
|
| 79 | struct bpf_array *array;
|
| 80 | u64 array_size, mask64;
|
| 81 |
|
| 82 | elem_size = round_up(attr->value_size, 8);
|
| 83 |
|
| 84 | max_entries = attr->max_entries;
|
| 85 |
|
| 86 | |
| 87 | * upper most bit set in u32 space is undefined behavior due to
|
| 88 | * resulting 1U << 32, so do it manually here in u64 space.
|
| 89 |
|
| 90 | mask64 = fls_long(max_entries - 1);
|
| 91 | mask64 = 1ULL << mask64;
|
| 92 | mask64 -= 1;
|
| 93 |
|
| 94 | index_mask = mask64;
|
| 95 | if (unpriv) {
|
| 96 | |
| 97 | * since cpu will speculate within index_mask limits
|
| 98 |
|
| 99 | max_entries = index_mask + 1;
|
| 100 |
|
| 101 | if (max_entries < attr->max_entries)
|
| 102 | return ERR_PTR(-E2BIG);
|
| 103 | }
|
| 104 |
|
| 105 | array_size = sizeof(*array);
|
| 106 | if (percpu)
|
| 107 | array_size += (u64) max_entries * sizeof(void *);
|
| 108 | else
|
| 109 | array_size += (u64) max_entries * elem_size;
|
| 110 |
|
| 111 |
|
| 112 | if (array_size >= U32_MAX - PAGE_SIZE)
|
| 113 | return ERR_PTR(-ENOMEM);
|
| 114 |
|
| 115 |
|
| 116 | array = bpf_map_area_alloc(array_size, numa_node);
|
| 117 | if (!array)
|
| 118 | return ERR_PTR(-ENOMEM);
|
| 119 | array->index_mask = index_mask;
|
| 120 | array->map.unpriv_array = unpriv;
|
| 121 |
|
| 122 |
|
| 123 | bpf_map_init_from_attr(&array->map, attr);
|
| 124 | array->elem_size = elem_size;
|
| 125 |
|
| 126 | if (!percpu)
|
| 127 | goto out;
|
| 128 |
|
| 129 | array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
|
| 130 |
|
| 131 | if (array_size >= U32_MAX - PAGE_SIZE ||
|
| 132 | bpf_array_alloc_percpu(array)) {
|
| 133 | bpf_map_area_free(array);
|
| 134 | return ERR_PTR(-ENOMEM);
|
| 135 | }
|
| 136 | out:
|
| 137 | array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
|
| 138 |
|
| 139 | return &array->map;
|
| 140 | }
|
| 141 |
|
| 142 |
|
| 143 | static void *array_map_lookup_elem(struct bpf_map *map, void *key)
|
| 144 | {
|
| 145 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 146 | u32 index = *(u32 *)key;
|
| 147 |
|
| 148 | if (unlikely(index >= array->map.max_entries))
|
| 149 | return NULL;
|
| 150 |
|
| 151 | return array->value + array->elem_size * (index & array->index_mask);
|
| 152 | }
|
| 153 |
|
| 154 |
|
| 155 | static u32 array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
|
| 156 | {
|
| 157 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 158 | struct bpf_insn *insn = insn_buf;
|
| 159 | u32 elem_size = round_up(map->value_size, 8);
|
| 160 | const int ret = BPF_REG_0;
|
| 161 | const int map_ptr = BPF_REG_1;
|
| 162 | const int index = BPF_REG_2;
|
| 163 |
|
| 164 | *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
|
| 165 | *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
|
| 166 | if (map->unpriv_array) {
|
| 167 | *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 4);
|
| 168 | *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
|
| 169 | } else {
|
| 170 | *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3);
|
| 171 | }
|
| 172 |
|
| 173 | if (is_power_of_2(elem_size)) {
|
| 174 | *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
|
| 175 | } else {
|
| 176 | *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
|
| 177 | }
|
| 178 | *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
|
| 179 | *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
|
| 180 | *insn++ = BPF_MOV64_IMM(ret, 0);
|
| 181 | return insn - insn_buf;
|
| 182 | }
|
| 183 |
|
| 184 |
|
| 185 | static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
|
| 186 | {
|
| 187 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 188 | u32 index = *(u32 *)key;
|
| 189 |
|
| 190 | if (unlikely(index >= array->map.max_entries))
|
| 191 | return NULL;
|
| 192 |
|
| 193 | return this_cpu_ptr(array->pptrs[index & array->index_mask]);
|
| 194 | }
|
| 195 |
|
| 196 | int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
|
| 197 | {
|
| 198 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 199 | u32 index = *(u32 *)key;
|
| 200 | void __percpu *pptr;
|
| 201 | int cpu, off = 0;
|
| 202 | u32 size;
|
| 203 |
|
| 204 | if (unlikely(index >= array->map.max_entries))
|
| 205 | return -ENOENT;
|
| 206 |
|
| 207 | |
| 208 | * access 'value_size' of them, so copying rounded areas
|
| 209 | * will not leak any kernel data
|
| 210 |
|
| 211 | size = round_up(map->value_size, 8);
|
| 212 | rcu_read_lock();
|
| 213 | pptr = array->pptrs[index & array->index_mask];
|
| 214 | for_each_possible_cpu(cpu) {
|
| 215 | bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
|
| 216 | off += size;
|
| 217 | }
|
| 218 | rcu_read_unlock();
|
| 219 | return 0;
|
| 220 | }
|
| 221 |
|
| 222 |
|
| 223 | static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
|
| 224 | {
|
| 225 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 226 | u32 index = key ? *(u32 *)key : U32_MAX;
|
| 227 | u32 *next = (u32 *)next_key;
|
| 228 |
|
| 229 | if (index >= array->map.max_entries) {
|
| 230 | *next = 0;
|
| 231 | return 0;
|
| 232 | }
|
| 233 |
|
| 234 | if (index == array->map.max_entries - 1)
|
| 235 | return -ENOENT;
|
| 236 |
|
| 237 | *next = index + 1;
|
| 238 | return 0;
|
| 239 | }
|
| 240 |
|
| 241 |
|
| 242 | static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
|
| 243 | u64 map_flags)
|
| 244 | {
|
| 245 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 246 | u32 index = *(u32 *)key;
|
| 247 |
|
| 248 | if (unlikely(map_flags > BPF_EXIST))
|
| 249 |
|
| 250 | return -EINVAL;
|
| 251 |
|
| 252 | if (unlikely(index >= array->map.max_entries))
|
| 253 |
|
| 254 | return -E2BIG;
|
| 255 |
|
| 256 | if (unlikely(map_flags == BPF_NOEXIST))
|
| 257 |
|
| 258 | return -EEXIST;
|
| 259 |
|
| 260 | if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
|
| 261 | memcpy(this_cpu_ptr(array->pptrs[index & array->index_mask]),
|
| 262 | value, map->value_size);
|
| 263 | else
|
| 264 | memcpy(array->value +
|
| 265 | array->elem_size * (index & array->index_mask),
|
| 266 | value, map->value_size);
|
| 267 | return 0;
|
| 268 | }
|
| 269 |
|
| 270 | int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
|
| 271 | u64 map_flags)
|
| 272 | {
|
| 273 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 274 | u32 index = *(u32 *)key;
|
| 275 | void __percpu *pptr;
|
| 276 | int cpu, off = 0;
|
| 277 | u32 size;
|
| 278 |
|
| 279 | if (unlikely(map_flags > BPF_EXIST))
|
| 280 |
|
| 281 | return -EINVAL;
|
| 282 |
|
| 283 | if (unlikely(index >= array->map.max_entries))
|
| 284 |
|
| 285 | return -E2BIG;
|
| 286 |
|
| 287 | if (unlikely(map_flags == BPF_NOEXIST))
|
| 288 |
|
| 289 | return -EEXIST;
|
| 290 |
|
| 291 | |
| 292 | * will be copied into per-cpu area. bpf programs can only access
|
| 293 | * value_size of it. During lookup the same extra bytes will be
|
| 294 | * returned or zeros which were zero-filled by percpu_alloc,
|
| 295 | * so no kernel data leaks possible
|
| 296 |
|
| 297 | size = round_up(map->value_size, 8);
|
| 298 | rcu_read_lock();
|
| 299 | pptr = array->pptrs[index & array->index_mask];
|
| 300 | for_each_possible_cpu(cpu) {
|
| 301 | bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
|
| 302 | off += size;
|
| 303 | }
|
| 304 | rcu_read_unlock();
|
| 305 | return 0;
|
| 306 | }
|
| 307 |
|
| 308 |
|
| 309 | static int array_map_delete_elem(struct bpf_map *map, void *key)
|
| 310 | {
|
| 311 | return -EINVAL;
|
| 312 | }
|
| 313 |
|
| 314 |
|
| 315 | static void array_map_free(struct bpf_map *map)
|
| 316 | {
|
| 317 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 318 |
|
| 319 | |
| 320 | * so the programs (can be more than one that used this map) were
|
| 321 | * disconnected from events. Wait for outstanding programs to complete
|
| 322 | * and free the array
|
| 323 |
|
| 324 | synchronize_rcu();
|
| 325 |
|
| 326 | if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
|
| 327 | bpf_array_free_percpu(array);
|
| 328 |
|
| 329 | bpf_map_area_free(array);
|
| 330 | }
|
| 331 |
|
| 332 | const struct bpf_map_ops array_map_ops = {
|
| 333 | .map_alloc_check = array_map_alloc_check,
|
| 334 | .map_alloc = array_map_alloc,
|
| 335 | .map_free = array_map_free,
|
| 336 | .map_get_next_key = array_map_get_next_key,
|
| 337 | .map_lookup_elem = array_map_lookup_elem,
|
| 338 | .map_update_elem = array_map_update_elem,
|
| 339 | .map_delete_elem = array_map_delete_elem,
|
| 340 | .map_gen_lookup = array_map_gen_lookup,
|
| 341 | };
|
| 342 |
|
| 343 | const struct bpf_map_ops percpu_array_map_ops = {
|
| 344 | .map_alloc_check = array_map_alloc_check,
|
| 345 | .map_alloc = array_map_alloc,
|
| 346 | .map_free = array_map_free,
|
| 347 | .map_get_next_key = array_map_get_next_key,
|
| 348 | .map_lookup_elem = percpu_array_map_lookup_elem,
|
| 349 | .map_update_elem = array_map_update_elem,
|
| 350 | .map_delete_elem = array_map_delete_elem,
|
| 351 | };
|
| 352 |
|
| 353 | static int fd_array_map_alloc_check(union bpf_attr *attr)
|
| 354 | {
|
| 355 |
|
| 356 | if (attr->value_size != sizeof(u32))
|
| 357 | return -EINVAL;
|
| 358 | return array_map_alloc_check(attr);
|
| 359 | }
|
| 360 |
|
| 361 | static void fd_array_map_free(struct bpf_map *map)
|
| 362 | {
|
| 363 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 364 | int i;
|
| 365 |
|
| 366 | synchronize_rcu();
|
| 367 |
|
| 368 |
|
| 369 | for (i = 0; i < array->map.max_entries; i++)
|
| 370 | BUG_ON(array->ptrs[i] != NULL);
|
| 371 |
|
| 372 | bpf_map_area_free(array);
|
| 373 | }
|
| 374 |
|
| 375 | static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
|
| 376 | {
|
| 377 | return NULL;
|
| 378 | }
|
| 379 |
|
| 380 |
|
| 381 | int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
|
| 382 | {
|
| 383 | void **elem, *ptr;
|
| 384 | int ret = 0;
|
| 385 |
|
| 386 | if (!map->ops->map_fd_sys_lookup_elem)
|
| 387 | return -ENOTSUPP;
|
| 388 |
|
| 389 | rcu_read_lock();
|
| 390 | elem = array_map_lookup_elem(map, key);
|
| 391 | if (elem && (ptr = READ_ONCE(*elem)))
|
| 392 | *value = map->ops->map_fd_sys_lookup_elem(ptr);
|
| 393 | else
|
| 394 | ret = -ENOENT;
|
| 395 | rcu_read_unlock();
|
| 396 |
|
| 397 | return ret;
|
| 398 | }
|
| 399 |
|
| 400 |
|
| 401 | int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
|
| 402 | void *key, void *value, u64 map_flags)
|
| 403 | {
|
| 404 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 405 | void *new_ptr, *old_ptr;
|
| 406 | u32 index = *(u32 *)key, ufd;
|
| 407 |
|
| 408 | if (map_flags != BPF_ANY)
|
| 409 | return -EINVAL;
|
| 410 |
|
| 411 | if (index >= array->map.max_entries)
|
| 412 | return -E2BIG;
|
| 413 |
|
| 414 | ufd = *(u32 *)value;
|
| 415 | new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
|
| 416 | if (IS_ERR(new_ptr))
|
| 417 | return PTR_ERR(new_ptr);
|
| 418 |
|
| 419 | old_ptr = xchg(array->ptrs + index, new_ptr);
|
| 420 | if (old_ptr)
|
| 421 | map->ops->map_fd_put_ptr(old_ptr);
|
| 422 |
|
| 423 | return 0;
|
| 424 | }
|
| 425 |
|
| 426 | static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
|
| 427 | {
|
| 428 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 429 | void *old_ptr;
|
| 430 | u32 index = *(u32 *)key;
|
| 431 |
|
| 432 | if (index >= array->map.max_entries)
|
| 433 | return -E2BIG;
|
| 434 |
|
| 435 | old_ptr = xchg(array->ptrs + index, NULL);
|
| 436 | if (old_ptr) {
|
| 437 | map->ops->map_fd_put_ptr(old_ptr);
|
| 438 | return 0;
|
| 439 | } else {
|
| 440 | return -ENOENT;
|
| 441 | }
|
| 442 | }
|
| 443 |
|
| 444 | static void *prog_fd_array_get_ptr(struct bpf_map *map,
|
| 445 | struct file *map_file, int fd)
|
| 446 | {
|
| 447 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 448 | struct bpf_prog *prog = bpf_prog_get(fd);
|
| 449 |
|
| 450 | if (IS_ERR(prog))
|
| 451 | return prog;
|
| 452 |
|
| 453 | if (!bpf_prog_array_compatible(array, prog)) {
|
| 454 | bpf_prog_put(prog);
|
| 455 | return ERR_PTR(-EINVAL);
|
| 456 | }
|
| 457 |
|
| 458 | return prog;
|
| 459 | }
|
| 460 |
|
| 461 | static void prog_fd_array_put_ptr(void *ptr)
|
| 462 | {
|
| 463 | bpf_prog_put(ptr);
|
| 464 | }
|
| 465 |
|
| 466 | static u32 prog_fd_array_sys_lookup_elem(void *ptr)
|
| 467 | {
|
| 468 | return ((struct bpf_prog *)ptr)->aux->id;
|
| 469 | }
|
| 470 |
|
| 471 |
|
| 472 | void bpf_fd_array_map_clear(struct bpf_map *map)
|
| 473 | {
|
| 474 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 475 | int i;
|
| 476 |
|
| 477 | for (i = 0; i < array->map.max_entries; i++)
|
| 478 | fd_array_map_delete_elem(map, &i);
|
| 479 | }
|
| 480 |
|
| 481 | const struct bpf_map_ops prog_array_map_ops = {
|
| 482 | .map_alloc_check = fd_array_map_alloc_check,
|
| 483 | .map_alloc = array_map_alloc,
|
| 484 | .map_free = fd_array_map_free,
|
| 485 | .map_get_next_key = array_map_get_next_key,
|
| 486 | .map_lookup_elem = fd_array_map_lookup_elem,
|
| 487 | .map_delete_elem = fd_array_map_delete_elem,
|
| 488 | .map_fd_get_ptr = prog_fd_array_get_ptr,
|
| 489 | .map_fd_put_ptr = prog_fd_array_put_ptr,
|
| 490 | .map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
|
| 491 | };
|
| 492 |
|
| 493 | static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
|
| 494 | struct file *map_file)
|
| 495 | {
|
| 496 | struct bpf_event_entry *ee;
|
| 497 |
|
| 498 | ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
|
| 499 | if (ee) {
|
| 500 | ee->event = perf_file->private_data;
|
| 501 | ee->perf_file = perf_file;
|
| 502 | ee->map_file = map_file;
|
| 503 | }
|
| 504 |
|
| 505 | return ee;
|
| 506 | }
|
| 507 |
|
| 508 | static void __bpf_event_entry_free(struct rcu_head *rcu)
|
| 509 | {
|
| 510 | struct bpf_event_entry *ee;
|
| 511 |
|
| 512 | ee = container_of(rcu, struct bpf_event_entry, rcu);
|
| 513 | fput(ee->perf_file);
|
| 514 | kfree(ee);
|
| 515 | }
|
| 516 |
|
| 517 | static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
|
| 518 | {
|
| 519 | call_rcu(&ee->rcu, __bpf_event_entry_free);
|
| 520 | }
|
| 521 |
|
| 522 | static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
|
| 523 | struct file *map_file, int fd)
|
| 524 | {
|
| 525 | struct bpf_event_entry *ee;
|
| 526 | struct perf_event *event;
|
| 527 | struct file *perf_file;
|
| 528 | u64 value;
|
| 529 |
|
| 530 | perf_file = perf_event_get(fd);
|
| 531 | if (IS_ERR(perf_file))
|
| 532 | return perf_file;
|
| 533 |
|
| 534 | ee = ERR_PTR(-EOPNOTSUPP);
|
| 535 | event = perf_file->private_data;
|
| 536 | if (perf_event_read_local(event, &value, NULL, NULL) == -EOPNOTSUPP)
|
| 537 | goto err_out;
|
| 538 |
|
| 539 | ee = bpf_event_entry_gen(perf_file, map_file);
|
| 540 | if (ee)
|
| 541 | return ee;
|
| 542 | ee = ERR_PTR(-ENOMEM);
|
| 543 | err_out:
|
| 544 | fput(perf_file);
|
| 545 | return ee;
|
| 546 | }
|
| 547 |
|
| 548 | static void perf_event_fd_array_put_ptr(void *ptr)
|
| 549 | {
|
| 550 | bpf_event_entry_free_rcu(ptr);
|
| 551 | }
|
| 552 |
|
| 553 | static void perf_event_fd_array_release(struct bpf_map *map,
|
| 554 | struct file *map_file)
|
| 555 | {
|
| 556 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 557 | struct bpf_event_entry *ee;
|
| 558 | int i;
|
| 559 |
|
| 560 | rcu_read_lock();
|
| 561 | for (i = 0; i < array->map.max_entries; i++) {
|
| 562 | ee = READ_ONCE(array->ptrs[i]);
|
| 563 | if (ee && ee->map_file == map_file)
|
| 564 | fd_array_map_delete_elem(map, &i);
|
| 565 | }
|
| 566 | rcu_read_unlock();
|
| 567 | }
|
| 568 |
|
| 569 | const struct bpf_map_ops perf_event_array_map_ops = {
|
| 570 | .map_alloc_check = fd_array_map_alloc_check,
|
| 571 | .map_alloc = array_map_alloc,
|
| 572 | .map_free = fd_array_map_free,
|
| 573 | .map_get_next_key = array_map_get_next_key,
|
| 574 | .map_lookup_elem = fd_array_map_lookup_elem,
|
| 575 | .map_delete_elem = fd_array_map_delete_elem,
|
| 576 | .map_fd_get_ptr = perf_event_fd_array_get_ptr,
|
| 577 | .map_fd_put_ptr = perf_event_fd_array_put_ptr,
|
| 578 | .map_release = perf_event_fd_array_release,
|
| 579 | };
|
| 580 |
|
| 581 | #ifdef CONFIG_CGROUPS
|
| 582 | static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
|
| 583 | struct file *map_file ,
|
| 584 | int fd)
|
| 585 | {
|
| 586 | return cgroup_get_from_fd(fd);
|
| 587 | }
|
| 588 |
|
| 589 | static void cgroup_fd_array_put_ptr(void *ptr)
|
| 590 | {
|
| 591 |
|
| 592 | cgroup_put(ptr);
|
| 593 | }
|
| 594 |
|
| 595 | static void cgroup_fd_array_free(struct bpf_map *map)
|
| 596 | {
|
| 597 | bpf_fd_array_map_clear(map);
|
| 598 | fd_array_map_free(map);
|
| 599 | }
|
| 600 |
|
| 601 | const struct bpf_map_ops cgroup_array_map_ops = {
|
| 602 | .map_alloc_check = fd_array_map_alloc_check,
|
| 603 | .map_alloc = array_map_alloc,
|
| 604 | .map_free = cgroup_fd_array_free,
|
| 605 | .map_get_next_key = array_map_get_next_key,
|
| 606 | .map_lookup_elem = fd_array_map_lookup_elem,
|
| 607 | .map_delete_elem = fd_array_map_delete_elem,
|
| 608 | .map_fd_get_ptr = cgroup_fd_array_get_ptr,
|
| 609 | .map_fd_put_ptr = cgroup_fd_array_put_ptr,
|
| 610 | };
|
| 611 | #endif
|
| 612 |
|
| 613 | static struct bpf_map *array_of_map_alloc(union bpf_attr *attr)
|
| 614 | {
|
| 615 | struct bpf_map *map, *inner_map_meta;
|
| 616 |
|
| 617 | inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
|
| 618 | if (IS_ERR(inner_map_meta))
|
| 619 | return inner_map_meta;
|
| 620 |
|
| 621 | map = array_map_alloc(attr);
|
| 622 | if (IS_ERR(map)) {
|
| 623 | bpf_map_meta_free(inner_map_meta);
|
| 624 | return map;
|
| 625 | }
|
| 626 |
|
| 627 | map->inner_map_meta = inner_map_meta;
|
| 628 |
|
| 629 | return map;
|
| 630 | }
|
| 631 |
|
| 632 | static void array_of_map_free(struct bpf_map *map)
|
| 633 | {
|
| 634 | |
| 635 | * is protected by fdget/fdput.
|
| 636 |
|
| 637 | bpf_map_meta_free(map->inner_map_meta);
|
| 638 | bpf_fd_array_map_clear(map);
|
| 639 | fd_array_map_free(map);
|
| 640 | }
|
| 641 |
|
| 642 | static void *array_of_map_lookup_elem(struct bpf_map *map, void *key)
|
| 643 | {
|
| 644 | struct bpf_map **inner_map = array_map_lookup_elem(map, key);
|
| 645 |
|
| 646 | if (!inner_map)
|
| 647 | return NULL;
|
| 648 |
|
| 649 | return READ_ONCE(*inner_map);
|
| 650 | }
|
| 651 |
|
| 652 | static u32 array_of_map_gen_lookup(struct bpf_map *map,
|
| 653 | struct bpf_insn *insn_buf)
|
| 654 | {
|
| 655 | struct bpf_array *array = container_of(map, struct bpf_array, map);
|
| 656 | u32 elem_size = round_up(map->value_size, 8);
|
| 657 | struct bpf_insn *insn = insn_buf;
|
| 658 | const int ret = BPF_REG_0;
|
| 659 | const int map_ptr = BPF_REG_1;
|
| 660 | const int index = BPF_REG_2;
|
| 661 |
|
| 662 | *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
|
| 663 | *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
|
| 664 | if (map->unpriv_array) {
|
| 665 | *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 6);
|
| 666 | *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
|
| 667 | } else {
|
| 668 | *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 5);
|
| 669 | }
|
| 670 | if (is_power_of_2(elem_size))
|
| 671 | *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
|
| 672 | else
|
| 673 | *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
|
| 674 | *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
|
| 675 | *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
|
| 676 | *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
|
| 677 | *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
|
| 678 | *insn++ = BPF_MOV64_IMM(ret, 0);
|
| 679 |
|
| 680 | return insn - insn_buf;
|
| 681 | }
|
| 682 |
|
| 683 | const struct bpf_map_ops array_of_maps_map_ops = {
|
| 684 | .map_alloc_check = fd_array_map_alloc_check,
|
| 685 | .map_alloc = array_of_map_alloc,
|
| 686 | .map_free = array_of_map_free,
|
| 687 | .map_get_next_key = array_map_get_next_key,
|
| 688 | .map_lookup_elem = array_of_map_lookup_elem,
|
| 689 | .map_delete_elem = fd_array_map_delete_elem,
|
| 690 | .map_fd_get_ptr = bpf_map_fd_get_ptr,
|
| 691 | .map_fd_put_ptr = bpf_map_fd_put_ptr,
|
| 692 | .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
|
| 693 | .map_gen_lookup = array_of_map_gen_lookup,
|
| 694 | };
|