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