Lines Matching defs:map

1029 static bool bpf_map__is_struct_ops(const struct bpf_map *map)
1031 return map->def.type == BPF_MAP_TYPE_STRUCT_OPS;
1034 /* Init the map's fields that depend on kern_btf */
1035 static int bpf_map__init_kern_struct_ops(struct bpf_map *map,
1047 st_ops = map->st_ops;
1058 map->name, st_ops->type_id, kern_type_id, kern_vtype_id);
1060 map->def.value_size = kern_vtype->size;
1061 map->btf_vmlinux_value_type_id = kern_vtype_id;
1085 map->name, mname);
1093 map->name, mname);
1109 map->name, mname, BTF_INFO_KIND(mtype->info),
1131 map->name, mname);
1141 map->name, mname, prog->name, moff,
1151 map->name, mname, (ssize_t)msize,
1157 map->name, mname, (unsigned int)msize,
1167 struct bpf_map *map;
1172 map = &obj->maps[i];
1174 if (!bpf_map__is_struct_ops(map))
1177 err = bpf_map__init_kern_struct_ops(map, obj->btf,
1195 struct bpf_map *map;
1234 map = bpf_object__add_map(obj);
1235 if (IS_ERR(map))
1236 return PTR_ERR(map);
1238 map->sec_idx = shndx;
1239 map->sec_offset = vsi->offset;
1240 map->name = strdup(var_name);
1241 if (!map->name)
1244 map->def.type = BPF_MAP_TYPE_STRUCT_OPS;
1245 map->def.key_size = sizeof(int);
1246 map->def.value_size = type->size;
1247 map->def.max_entries = 1;
1248 map->def.map_flags = map_flags;
1250 map->st_ops = calloc(1, sizeof(*map->st_ops));
1251 if (!map->st_ops)
1253 st_ops = map->st_ops;
1581 struct bpf_map *map;
1589 map = &obj->maps[obj->nr_maps++];
1590 map->obj = obj;
1591 map->fd = -1;
1592 map->inner_map_fd = -1;
1593 map->autocreate = true;
1595 return map;
1608 static int bpf_map_mmap_resize(struct bpf_map *map, size_t old_sz, size_t new_sz)
1612 if (!map->mmaped)
1622 memcpy(mmaped, map->mmaped, min(old_sz, new_sz));
1623 munmap(map->mmaped, old_sz);
1624 map->mmaped = mmaped;
1639 * and internal map corresponding to '.rodata' ELF section. The final
1640 * map name advertised to user and to the kernel will be
1643 * Somewhat confusingly, if internal map ELF section name is shorter
1646 * resulting map will be called 'my_objec.bss', not even using all 15
1648 * object name is somewhat consistent in this case. But if the map
1651 * object name ('my_obje'). Happy guessing, user, that the final map
1664 * BTF type associated with such map's value type, though.
1669 /* if there are two or more dots in map name, it's a custom dot map */
1678 /* sanitise map name to characters allowed by kernel */
1687 map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map);
1689 /* Internal BPF map is mmap()'able only if at least one of corresponding
1694 static bool map_is_mmapable(struct bpf_object *obj, struct bpf_map *map)
1700 if (!map->btf_value_type_id)
1703 t = btf__type_by_id(obj->btf, map->btf_value_type_id);
1725 struct bpf_map *map;
1729 map = bpf_object__add_map(obj);
1730 if (IS_ERR(map))
1731 return PTR_ERR(map);
1733 map->libbpf_type = type;
1734 map->sec_idx = sec_idx;
1735 map->sec_offset = 0;
1736 map->real_name = strdup(real_name);
1737 map->name = internal_map_name(obj, real_name);
1738 if (!map->real_name || !map->name) {
1739 zfree(&map->real_name);
1740 zfree(&map->name);
1744 def = &map->def;
1753 (void) map_fill_btf_type_info(obj, map);
1755 if (map_is_mmapable(obj, map))
1758 pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n",
1759 map->name, map->sec_idx, map->sec_offset, def->map_flags);
1761 mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
1762 map->mmaped = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE,
1764 if (map->mmaped == MAP_FAILED) {
1766 map->mmaped = NULL;
1767 pr_warn("failed to alloc map '%s' content buffer: %d\n",
1768 map->name, err);
1769 zfree(&map->real_name);
1770 zfree(&map->name);
1775 memcpy(map->mmaped, data, data_sz);
1777 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
2225 * Fetch integer attribute of BTF map definition. Such attributes are
2240 pr_warn("map '%s': attr '%s': expected PTR, got %s.\n",
2247 pr_warn("map '%s': attr '%s': type [%u] not found.\n",
2252 pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n",
2274 static int build_map_pin_path(struct bpf_map *map, const char *path)
2282 err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
2286 return bpf_map__set_pin_path(map, buf);
2311 pr_warn("map '%s': invalid field #%d.\n", map_name, i);
2336 pr_warn("map '%s': conflicting key size %u != %u.\n",
2347 pr_warn("map '%s': key type [%d] not found.\n",
2352 pr_warn("map '%s': key spec is not PTR: %s.\n",
2358 pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n",
2363 pr_warn("map '%s': conflicting key size %u != %zd.\n",
2376 pr_warn("map '%s': conflicting value size %u != %u.\n",
2387 pr_warn("map '%s': value type [%d] not found.\n",
2392 pr_warn("map '%s': value spec is not PTR: %s.\n",
2398 pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n",
2403 pr_warn("map '%s': conflicting value size %u != %zd.\n",
2414 const char *desc = is_map_in_map ? "map-in-map inner" : "prog-array value";
2419 pr_warn("map '%s': multi-level inner maps not supported.\n",
2424 pr_warn("map '%s': '%s' member should be last.\n",
2429 pr_warn("map '%s': should be map-in-map or prog-array.\n",
2434 pr_warn("map '%s': conflicting value size %u != 4.\n",
2441 pr_warn("map '%s': %s type [%d] not found.\n",
2446 pr_warn("map '%s': %s spec is not a zero-sized array.\n",
2452 pr_warn("map '%s': %s def is of unexpected kind %s.\n",
2459 pr_warn("map '%s': prog-array value def is of unexpected kind %s.\n",
2466 pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
2481 pr_warn("map '%s': inner def can't be pinned.\n", map_name);
2487 pr_warn("map '%s': invalid pinning value %u.\n",
2502 pr_warn("map '%s': unknown field '%s'.\n", map_name, name);
2505 pr_debug("map '%s': ignoring unknown field '%s'.\n", map_name, name);
2510 pr_warn("map '%s': map type isn't specified.\n", map_name);
2534 * requirements and substitute correct max_entries for map creation.
2548 static bool map_is_ringbuf(const struct bpf_map *map)
2550 return map->def.type == BPF_MAP_TYPE_RINGBUF ||
2551 map->def.type == BPF_MAP_TYPE_USER_RINGBUF;
2554 static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def)
2556 map->def.type = def->map_type;
2557 map->def.key_size = def->key_size;
2558 map->def.value_size = def->value_size;
2559 map->def.max_entries = def->max_entries;
2560 map->def.map_flags = def->map_flags;
2561 map->map_extra = def->map_extra;
2563 map->numa_node = def->numa_node;
2564 map->btf_key_type_id = def->key_type_id;
2565 map->btf_value_type_id = def->value_type_id;
2567 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */
2568 if (map_is_ringbuf(map))
2569 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries);
2572 pr_debug("map '%s': found type = %u.\n", map->name, def->map_type);
2575 pr_debug("map '%s': found key [%u], sz = %u.\n",
2576 map->name, def->key_type_id, def->key_size);
2578 pr_debug("map '%s': found key_size = %u.\n", map->name, def->key_size);
2581 pr_debug("map '%s': found value [%u], sz = %u.\n",
2582 map->name, def->value_type_id, def->value_size);
2584 pr_debug("map '%s': found value_size = %u.\n", map->name, def->value_size);
2587 pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries);
2589 pr_debug("map '%s': found map_flags = 0x%x.\n", map->name, def->map_flags);
2591 pr_debug("map '%s': found map_extra = 0x%llx.\n", map->name,
2594 pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning);
2596 pr_debug("map '%s': found numa_node = %u.\n", map->name, def->numa_node);
2599 pr_debug("map '%s': found inner map definition.\n", map->name);
2623 struct bpf_map *map;
2632 pr_warn("map #%d: empty name.\n", var_idx);
2636 pr_warn("map '%s' BTF data is corrupted.\n", map_name);
2640 pr_warn("map '%s': unexpected var kind %s.\n",
2645 pr_warn("map '%s': unsupported map linkage %s.\n",
2652 pr_warn("map '%s': unexpected def kind %s.\n",
2657 pr_warn("map '%s': invalid def size.\n", map_name);
2661 map = bpf_object__add_map(obj);
2662 if (IS_ERR(map))
2663 return PTR_ERR(map);
2664 map->name = strdup(map_name);
2665 if (!map->name) {
2666 pr_warn("map '%s': failed to alloc map name.\n", map_name);
2669 map->libbpf_type = LIBBPF_MAP_UNSPEC;
2670 map->def.type = BPF_MAP_TYPE_UNSPEC;
2671 map->sec_idx = sec_idx;
2672 map->sec_offset = vi->offset;
2673 map->btf_var_idx = var_idx;
2674 pr_debug("map '%s': at sec_idx %d, offset %zu.\n",
2675 map_name, map->sec_idx, map->sec_offset);
2677 err = parse_btf_map_def(map->name, obj->btf, def, strict, &map_def, &inner_def);
2681 fill_map_from_def(map, &map_def);
2684 err = build_map_pin_path(map, pin_root_path);
2686 pr_warn("map '%s': couldn't build pin path.\n", map->name);
2692 map->inner_map = calloc(1, sizeof(*map->inner_map));
2693 if (!map->inner_map)
2695 map->inner_map->fd = -1;
2696 map->inner_map->sec_idx = sec_idx;
2697 map->inner_map->name = malloc(strlen(map_name) + sizeof(".inner") + 1);
2698 if (!map->inner_map->name)
2700 sprintf(map->inner_map->name, "%s.inner", map_name);
2702 fill_map_from_def(map->inner_map, &inner_def);
2705 err = map_fill_btf_type_info(obj, map);
2735 pr_warn("elf: failed to get %s map definitions for %s\n",
3823 pr_warn("elf: legacy map definitions in 'maps' section are not supported by libbpf v1.0+\n");
4343 /* for kcfg externs calculate their offsets within a .kconfig map */
4443 struct bpf_map *map;
4534 /* generic map reference relocation */
4537 pr_warn("prog '%s': bad map relo against '%s' in section '%s'\n",
4542 map = &obj->maps[map_idx];
4543 if (map->libbpf_type != type ||
4544 map->sec_idx != sym->st_shndx ||
4545 map->sec_offset != sym->st_value)
4547 pr_debug("prog '%s': found map %zd (%s, sec %d, off %zu) for insn #%u\n",
4548 prog->name, map_idx, map->name, map->sec_idx,
4549 map->sec_offset, insn_idx);
4553 pr_warn("prog '%s': map relo failed to find map for section '%s', off %zu\n",
4564 /* global data map relocation */
4571 map = &obj->maps[map_idx];
4572 if (map->libbpf_type != type || map->sec_idx != sym->st_shndx)
4574 pr_debug("prog '%s': found data map %zd (%s, sec %d, off %zu) for insn %u\n",
4575 prog->name, map_idx, map->name, map->sec_idx,
4576 map->sec_offset, insn_idx);
4580 pr_warn("prog '%s': data relo failed to find map for section '%s'\n",
4740 static int map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map)
4747 /* if it's BTF-defined map, we don't need to search for type IDs.
4748 * For struct_ops map, it does not need btf_key_type_id and
4751 if (map->sec_idx == obj->efile.btf_maps_shndx || bpf_map__is_struct_ops(map))
4758 if (!bpf_map__is_internal(map))
4761 id = btf__find_by_name(obj->btf, map->real_name);
4765 map->btf_key_type_id = 0;
4766 map->btf_value_type_id = id;
4806 bool bpf_map__autocreate(const struct bpf_map *map)
4808 return map->autocreate;
4811 int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate)
4813 if (map->obj->loaded)
4816 map->autocreate = autocreate;
4820 int bpf_map__reuse_fd(struct bpf_map *map, int fd)
4835 if (name_len == BPF_OBJ_NAME_LEN - 1 && strncmp(map->name, info.name, name_len) == 0)
4836 new_name = strdup(map->name);
4854 err = zclose(map->fd);
4859 free(map->name);
4861 map->fd = new_fd;
4862 map->name = new_name;
4863 map->def.type = info.type;
4864 map->def.key_size = info.key_size;
4865 map->def.value_size = info.value_size;
4866 map->def.max_entries = info.max_entries;
4867 map->def.map_flags = info.map_flags;
4868 map->btf_key_type_id = info.btf_key_type_id;
4869 map->btf_value_type_id = info.btf_value_type_id;
4870 map->reused = true;
4871 map->map_extra = info.map_extra;
4882 __u32 bpf_map__max_entries(const struct bpf_map *map)
4884 return map->def.max_entries;
4887 struct bpf_map *bpf_map__inner_map(struct bpf_map *map)
4889 if (!bpf_map_type__is_map_in_map(map->def.type))
4892 return map->inner_map;
4895 int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries)
4897 if (map->obj->loaded)
4900 map->def.max_entries = max_entries;
4902 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */
4903 if (map_is_ringbuf(map))
4904 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries);
4982 int ret, map, insn_cnt = ARRAY_SIZE(insns);
4984 map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_global", sizeof(int), 32, 1, NULL);
4985 if (map < 0) {
4988 pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
4993 insns[0].imm = map;
4996 close(map);
5162 int ret, map, prog, insn_cnt = ARRAY_SIZE(insns);
5164 map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_det_bind", sizeof(int), 32, 1, NULL);
5165 if (map < 0) {
5168 pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
5175 close(map);
5179 ret = bpf_prog_bind_map(prog, map, NULL);
5181 close(map);
5332 "ARRAY map mmap()", probe_kern_array_mmap,
5402 static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
5414 pr_warn("failed to get map info for map FD %d: %s\n", map_fd,
5419 return (map_info.type == map->def.type &&
5420 map_info.key_size == map->def.key_size &&
5421 map_info.value_size == map->def.value_size &&
5422 map_info.max_entries == map->def.max_entries &&
5423 map_info.map_flags == map->def.map_flags &&
5424 map_info.map_extra == map->map_extra);
5428 bpf_object__reuse_map(struct bpf_map *map)
5433 pin_fd = bpf_obj_get(map->pin_path);
5437 pr_debug("found no pinned map to reuse at '%s'\n",
5438 map->pin_path);
5443 pr_warn("couldn't retrieve pinned map '%s': %s\n",
5444 map->pin_path, cp);
5448 if (!map_is_reuse_compat(map, pin_fd)) {
5449 pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n",
5450 map->pin_path);
5455 err = bpf_map__reuse_fd(map, pin_fd);
5460 map->pinned = true;
5461 pr_debug("reused pinned map at '%s'\n", map->pin_path);
5467 bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
5469 enum libbpf_map_type map_type = map->libbpf_type;
5474 bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps,
5475 map->mmaped, map->def.value_size);
5477 bpf_gen__map_freeze(obj->gen_loader, map - obj->maps);
5480 err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0);
5484 pr_warn("Error setting initial map(%s) contents: %s\n",
5485 map->name, cp);
5489 /* Freeze .rodata and .kconfig map as read-only from syscall side. */
5491 err = bpf_map_freeze(map->fd);
5495 pr_warn("Error freezing map(%s) as read-only: %s\n",
5496 map->name, cp);
5503 static void bpf_map__destroy(struct bpf_map *map);
5505 static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner)
5508 struct bpf_map_def *def = &map->def;
5513 map_name = map->name;
5514 create_attr.map_ifindex = map->map_ifindex;
5516 create_attr.numa_node = map->numa_node;
5517 create_attr.map_extra = map->map_extra;
5519 if (bpf_map__is_struct_ops(map))
5520 create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
5524 create_attr.btf_key_type_id = map->btf_key_type_id;
5525 create_attr.btf_value_type_id = map->btf_value_type_id;
5529 if (map->inner_map) {
5530 err = bpf_object__create_map(obj, map->inner_map, true);
5532 pr_warn("map '%s': failed to create inner map: %d\n",
5533 map->name, err);
5536 map->inner_map_fd = bpf_map__fd(map->inner_map);
5538 if (map->inner_map_fd >= 0)
5539 create_attr.inner_map_fd = map->inner_map_fd;
5559 map->btf_key_type_id = 0;
5560 map->btf_value_type_id = 0;
5569 &create_attr, is_inner ? -1 : map - obj->maps);
5573 map->fd = 0;
5575 map->fd = bpf_map_create(def->type, map_name,
5579 if (map->fd < 0 && (create_attr.btf_key_type_id ||
5586 map->name, cp, err);
5590 map->btf_key_type_id = 0;
5591 map->btf_value_type_id = 0;
5592 map->fd = bpf_map_create(def->type, map_name,
5597 err = map->fd < 0 ? -errno : 0;
5599 if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) {
5601 map->inner_map->fd = -1;
5602 bpf_map__destroy(map->inner_map);
5603 zfree(&map->inner_map);
5609 static int init_map_in_map_slots(struct bpf_object *obj, struct bpf_map *map)
5615 for (i = 0; i < map->init_slots_sz; i++) {
5616 if (!map->init_slots[i])
5619 targ_map = map->init_slots[i];
5624 map - obj->maps, i,
5627 err = bpf_map_update_elem(map->fd, &i, &fd, 0);
5631 pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n",
5632 map->name, i, targ_map->name, fd, err);
5635 pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
5636 map->name, i, targ_map->name, fd);
5639 zfree(&map->init_slots);
5640 map->init_slots_sz = 0;
5645 static int init_prog_array_slots(struct bpf_object *obj, struct bpf_map *map)
5654 for (i = 0; i < map->init_slots_sz; i++) {
5655 if (!map->init_slots[i])
5658 targ_prog = map->init_slots[i];
5661 err = bpf_map_update_elem(map->fd, &i, &fd, 0);
5664 pr_warn("map '%s': failed to initialize slot [%d] to prog '%s' fd=%d: %d\n",
5665 map->name, i, targ_prog->name, fd, err);
5668 pr_debug("map '%s': slot [%d] set to prog '%s' fd=%d\n",
5669 map->name, i, targ_prog->name, fd);
5672 zfree(&map->init_slots);
5673 map->init_slots_sz = 0;
5680 struct bpf_map *map;
5684 map = &obj->maps[i];
5686 if (!map->init_slots_sz || map->def.type != BPF_MAP_TYPE_PROG_ARRAY)
5689 err = init_prog_array_slots(obj, map);
5691 zclose(map->fd);
5698 static int map_set_def_max_entries(struct bpf_map *map)
5700 if (map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !map->def.max_entries) {
5705 pr_warn("map '%s': failed to determine number of system CPUs: %d\n",
5706 map->name, nr_cpus);
5709 pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus);
5710 map->def.max_entries = nr_cpus;
5719 struct bpf_map *map;
5726 map = &obj->maps[i];
5731 * programs is referencing any global data map, we'll error
5742 if (bpf_map__is_internal(map) && !kernel_supports(obj, FEAT_GLOBAL_DATA))
5743 map->autocreate = false;
5745 if (!map->autocreate) {
5746 pr_debug("map '%s': skipped auto-creating...\n", map->name);
5750 err = map_set_def_max_entries(map);
5756 if (map->pin_path) {
5757 err = bpf_object__reuse_map(map);
5759 pr_warn("map '%s': error reusing pinned map\n",
5760 map->name);
5763 if (retried && map->fd < 0) {
5764 pr_warn("map '%s': cannot find pinned map\n",
5765 map->name);
5771 if (map->fd >= 0) {
5772 pr_debug("map '%s': skipping creation (preset fd=%d)\n",
5773 map->name, map->fd);
5775 err = bpf_object__create_map(obj, map, false);
5779 pr_debug("map '%s': created successfully, fd=%d\n",
5780 map->name, map->fd);
5782 if (bpf_map__is_internal(map)) {
5783 err = bpf_object__populate_internal_map(obj, map);
5785 zclose(map->fd);
5790 if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) {
5791 err = init_map_in_map_slots(obj, map);
5793 zclose(map->fd);
5799 if (map->pin_path && !map->pinned) {
5800 err = bpf_map__pin(map, NULL);
5802 zclose(map->fd);
5807 pr_warn("map '%s': failed to auto-pin at '%s': %d\n",
5808 map->name, map->pin_path, err);
5818 pr_warn("map '%s': failed to create: %s(%d)\n", map->name, cp, err);
6306 /* base map load ldimm64 special constant, used also for log fixup logic */
6312 int map_idx, const struct bpf_map *map)
6316 pr_debug("prog '%s': relo #%d: poisoning insn #%d that loads map #%d '%s'\n",
6317 prog->name, relo_idx, insn_idx, map_idx, map->name);
6328 * where lower 123 is map index into obj->maps[] array
6361 * - map references;
6373 const struct bpf_map *map;
6378 map = &obj->maps[relo->map_idx];
6382 } else if (map->autocreate) {
6384 insn[0].imm = map->fd;
6387 relo->map_idx, map);
6391 map = &obj->maps[relo->map_idx];
6396 } else if (map->autocreate) {
6398 insn[0].imm = map->fd;
6401 relo->map_idx, map);
7044 struct bpf_map *map = NULL, *targ_map = NULL;
7081 map = &obj->maps[j];
7082 if (map->sec_idx != obj->efile.btf_maps_shndx)
7085 vi = btf_var_secinfos(sec) + map->btf_var_idx;
7091 pr_warn(".maps relo #%d: cannot find map '%s' at rel->r_offset %zu\n",
7096 is_map_in_map = bpf_map_type__is_map_in_map(map->def.type);
7097 is_prog_array = map->def.type == BPF_MAP_TYPE_PROG_ARRAY;
7098 type = is_map_in_map ? "map" : "prog";
7101 pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n",
7105 if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS &&
7106 map->def.key_size != sizeof(int)) {
7108 i, map->name, sizeof(int));
7113 pr_warn(".maps relo #%d: '%s' isn't a valid map reference\n",
7155 if (moff >= map->init_slots_sz) {
7157 tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz);
7160 map->init_slots = tmp;
7161 memset(map->init_slots + map->init_slots_sz, 0,
7162 (new_sz - map->init_slots_sz) * host_ptr_sz);
7163 map->init_slots_sz = new_sz;
7165 map->init_slots[moff] = is_map_in_map ? (void *)targ_map : (void *)targ_prog;
7167 pr_debug(".maps relo #%d: map '%s' slot [%d] points to %s '%s'\n",
7168 i, map->name, moff, type, name);
7440 struct bpf_map *map;
7444 map = &prog->obj->maps[i];
7445 if (map->libbpf_type != LIBBPF_MAP_RODATA)
7448 if (bpf_prog_bind_map(ret, bpf_map__fd(map), NULL)) {
7450 pr_warn("prog '%s': failed to bind map '%s': %s\n",
7451 prog->name, map->real_name, cp);
7590 /* Expected log for failed and not properly guarded map reference:
7596 * "345" in "2001000345" is a map index in obj->maps to fetch map name.
7599 const struct bpf_map *map;
7609 map = &obj->maps[map_idx];
7612 "%d: <invalid BPF map reference>\n"
7613 "BPF map '%s' is referenced but wasn't created\n",
7614 insn_idx, map->name);
7685 /* reference to uncreated BPF map */
8333 static void bpf_map_prepare_vdata(const struct bpf_map *map)
8338 st_ops = map->st_ops;
8540 int bpf_map__pin(struct bpf_map *map, const char *path)
8545 if (map == NULL) {
8546 pr_warn("invalid map pointer\n");
8550 if (map->pin_path) {
8551 if (path && strcmp(path, map->pin_path)) {
8552 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
8553 bpf_map__name(map), map->pin_path, path);
8555 } else if (map->pinned) {
8556 pr_debug("map '%s' already pinned at '%s'; not re-pinning\n",
8557 bpf_map__name(map), map->pin_path);
8562 pr_warn("missing a path to pin map '%s' at\n",
8563 bpf_map__name(map));
8565 } else if (map->pinned) {
8566 pr_warn("map '%s' already pinned\n", bpf_map__name(map));
8570 map->pin_path = strdup(path);
8571 if (!map->pin_path) {
8577 err = make_parent_dir(map->pin_path);
8581 err = check_path(map->pin_path);
8585 if (bpf_obj_pin(map->fd, map->pin_path)) {
8590 map->pinned = true;
8591 pr_debug("pinned map '%s'\n", map->pin_path);
8597 pr_warn("failed to pin map: %s\n", cp);
8601 int bpf_map__unpin(struct bpf_map *map, const char *path)
8605 if (map == NULL) {
8606 pr_warn("invalid map pointer\n");
8610 if (map->pin_path) {
8611 if (path && strcmp(path, map->pin_path)) {
8612 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
8613 bpf_map__name(map), map->pin_path, path);
8616 path = map->pin_path;
8618 pr_warn("no path to unpin map '%s' from\n",
8619 bpf_map__name(map));
8631 map->pinned = false;
8632 pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map), path);
8637 int bpf_map__set_pin_path(struct bpf_map *map, const char *path)
8647 free(map->pin_path);
8648 map->pin_path = new;
8653 const char *bpf_map__get_pin_path(const struct bpf_map *map);
8655 const char *bpf_map__pin_path(const struct bpf_map *map)
8657 return map->pin_path;
8660 bool bpf_map__is_pinned(const struct bpf_map *map)
8662 return map->pinned;
8677 struct bpf_map *map;
8688 bpf_object__for_each_map(map, obj) {
8692 if (!map->autocreate)
8696 err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
8701 } else if (!map->pin_path) {
8705 err = bpf_map__pin(map, pin_path);
8713 while ((map = bpf_object__prev_map(obj, map))) {
8714 if (!map->pin_path)
8717 bpf_map__unpin(map, NULL);
8725 struct bpf_map *map;
8731 bpf_object__for_each_map(map, obj) {
8736 err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
8741 } else if (!map->pin_path) {
8745 err = bpf_map__unpin(map, pin_path);
8845 static void bpf_map__destroy(struct bpf_map *map)
8847 if (map->inner_map) {
8848 bpf_map__destroy(map->inner_map);
8849 zfree(&map->inner_map);
8852 zfree(&map->init_slots);
8853 map->init_slots_sz = 0;
8855 if (map->mmaped) {
8858 mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
8859 munmap(map->mmaped, mmap_sz);
8860 map->mmaped = NULL;
8863 if (map->st_ops) {
8864 zfree(&map->st_ops->data);
8865 zfree(&map->st_ops->progs);
8866 zfree(&map->st_ops->kern_func_off);
8867 zfree(&map->st_ops);
8870 zfree(&map->name);
8871 zfree(&map->real_name);
8872 zfree(&map->pin_path);
8874 if (map->fd >= 0)
8875 zclose(map->fd);
9564 struct bpf_map *map;
9568 map = &obj->maps[i];
9569 if (!bpf_map__is_struct_ops(map))
9571 if (map->sec_idx == sec_idx &&
9572 map->sec_offset <= offset &&
9573 offset - map->sec_offset < map->def.value_size)
9574 return map;
9589 struct bpf_map *map;
9614 map = find_struct_ops_map_by_offset(obj, shdr->sh_info, rel->r_offset);
9615 if (!map) {
9616 pr_warn("struct_ops reloc: cannot find map at rel->r_offset %zu\n",
9621 moff = rel->r_offset - map->sec_offset;
9623 st_ops = map->st_ops;
9624 pr_debug("struct_ops reloc %s: for %lld value %lld shdr_idx %u rel->r_offset %zu map->sec_offset %zu name %d (\'%s\')\n",
9625 map->name,
9629 map->sec_offset, sym->st_name, name);
9633 map->name, (size_t)rel->r_offset, shdr_idx);
9638 map->name, (unsigned long long)sym->st_value);
9646 map->name, moff);
9654 map->name, name);
9661 map->name, shdr_idx, name);
9668 map->name, prog->name);
9688 map->name, prog->name, prog->sec_name, prog->type,
9918 int bpf_map__fd(const struct bpf_map *map)
9920 return map ? map->fd : libbpf_err(-EINVAL);
9923 static bool map_uses_real_name(const struct bpf_map *map)
9927 * such map's corresponding ELF section name as a map name.
9931 if (map->libbpf_type == LIBBPF_MAP_DATA && strcmp(map->real_name, DATA_SEC) != 0)
9933 if (map->libbpf_type == LIBBPF_MAP_RODATA && strcmp(map->real_name, RODATA_SEC) != 0)
9938 const char *bpf_map__name(const struct bpf_map *map)
9940 if (!map)
9943 if (map_uses_real_name(map))
9944 return map->real_name;
9946 return map->name;
9949 enum bpf_map_type bpf_map__type(const struct bpf_map *map)
9951 return map->def.type;
9954 int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type)
9956 if (map->fd >= 0)
9958 map->def.type = type;
9962 __u32 bpf_map__map_flags(const struct bpf_map *map)
9964 return map->def.map_flags;
9967 int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags)
9969 if (map->fd >= 0)
9971 map->def.map_flags = flags;
9975 __u64 bpf_map__map_extra(const struct bpf_map *map)
9977 return map->map_extra;
9980 int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra)
9982 if (map->fd >= 0)
9984 map->map_extra = map_extra;
9988 __u32 bpf_map__numa_node(const struct bpf_map *map)
9990 return map->numa_node;
9993 int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node)
9995 if (map->fd >= 0)
9997 map->numa_node = numa_node;
10001 __u32 bpf_map__key_size(const struct bpf_map *map)
10003 return map->def.key_size;
10006 int bpf_map__set_key_size(struct bpf_map *map, __u32 size)
10008 if (map->fd >= 0)
10010 map->def.key_size = size;
10014 __u32 bpf_map__value_size(const struct bpf_map *map)
10016 return map->def.value_size;
10019 static int map_btf_datasec_resize(struct bpf_map *map, __u32 size)
10030 btf = bpf_object__btf(map->obj);
10034 /* verify map is datasec */
10035 datasec_type = btf_type_by_id(btf, bpf_map__btf_value_type_id(map));
10037 pr_warn("map '%s': cannot be resized, map value type is not a datasec\n",
10038 bpf_map__name(map));
10045 pr_warn("map '%s': cannot be resized, map value datasec is empty\n",
10046 bpf_map__name(map));
10055 pr_warn("map '%s': cannot be resized, last var must be an array\n",
10056 bpf_map__name(map));
10064 pr_warn("map '%s': cannot be resized, element size (%d) doesn't align with new total size (%u)\n",
10065 bpf_map__name(map), element_sz, size);
10078 datasec_type = btf_type_by_id(btf, map->btf_value_type_id);
10090 int bpf_map__set_value_size(struct bpf_map *map, __u32 size)
10092 if (map->fd >= 0)
10095 if (map->mmaped) {
10099 mmap_old_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
10100 mmap_new_sz = bpf_map_mmap_sz(size, map->def.max_entries);
10101 err = bpf_map_mmap_resize(map, mmap_old_sz, mmap_new_sz);
10103 pr_warn("map '%s': failed to resize memory-mapped region: %d\n",
10104 bpf_map__name(map), err);
10107 err = map_btf_datasec_resize(map, size);
10109 pr_warn("map '%s': failed to adjust resized BTF, clearing BTF key/value info: %d\n",
10110 bpf_map__name(map), err);
10111 map->btf_value_type_id = 0;
10112 map->btf_key_type_id = 0;
10116 map->def.value_size = size;
10120 __u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
10122 return map ? map->btf_key_type_id : 0;
10125 __u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
10127 return map ? map->btf_value_type_id : 0;
10130 int bpf_map__set_initial_value(struct bpf_map *map,
10133 if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG ||
10134 size != map->def.value_size || map->fd >= 0)
10137 memcpy(map->mmaped, data, size);
10141 void *bpf_map__initial_value(struct bpf_map *map, size_t *psize)
10143 if (!map->mmaped)
10145 *psize = map->def.value_size;
10146 return map->mmaped;
10149 bool bpf_map__is_internal(const struct bpf_map *map)
10151 return map->libbpf_type != LIBBPF_MAP_UNSPEC;
10154 __u32 bpf_map__ifindex(const struct bpf_map *map)
10156 return map->map_ifindex;
10159 int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
10161 if (map->fd >= 0)
10163 map->map_ifindex = ifindex;
10167 int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
10169 if (!bpf_map_type__is_map_in_map(map->def.type)) {
10170 pr_warn("error: unsupported map type\n");
10173 if (map->inner_map_fd != -1) {
10177 if (map->inner_map) {
10178 bpf_map__destroy(map->inner_map);
10179 zfree(&map->inner_map);
10181 map->inner_map_fd = fd;
10198 pr_warn("error in %s: map handler doesn't belong to object\n",
10236 /* if it's a special internal map name (which always starts
10238 * real map name (ELF section name)
10245 /* otherwise map name has to be an exact match */
10263 static int validate_map_op(const struct bpf_map *map, size_t key_sz,
10266 if (map->fd <= 0)
10269 if (map->def.key_size != key_sz) {
10270 pr_warn("map '%s': unexpected key size %zu provided, expected %u\n",
10271 map->name, key_sz, map->def.key_size);
10278 switch (map->def.type) {
10284 size_t elem_sz = roundup(map->def.value_size, 8);
10287 pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n",
10288 map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz);
10294 if (map->def.value_size != value_sz) {
10295 pr_warn("map '%s': unexpected value size %zu provided, expected %u\n",
10296 map->name, value_sz, map->def.value_size);
10304 int bpf_map__lookup_elem(const struct bpf_map *map,
10310 err = validate_map_op(map, key_sz, value_sz, true);
10314 return bpf_map_lookup_elem_flags(map->fd, key, value, flags);
10317 int bpf_map__update_elem(const struct bpf_map *map,
10323 err = validate_map_op(map, key_sz, value_sz, true);
10327 return bpf_map_update_elem(map->fd, key, value, flags);
10330 int bpf_map__delete_elem(const struct bpf_map *map,
10335 err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10339 return bpf_map_delete_elem_flags(map->fd, key, flags);
10342 int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
10348 err = validate_map_op(map, key_sz, value_sz, true);
10352 return bpf_map_lookup_and_delete_elem_flags(map->fd, key, value, flags);
10355 int bpf_map__get_next_key(const struct bpf_map *map,
10360 err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10364 return bpf_map_get_next_key(map->fd, cur_key, next_key);
12678 struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)
12684 if (!bpf_map__is_struct_ops(map) || map->fd == -1)
12692 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0);
12693 /* It can be EBUSY if the map has been used to create or
12698 if (err && (!(map->def.map_flags & BPF_F_LINK) || err != -EBUSY)) {
12705 if (!(map->def.map_flags & BPF_F_LINK)) {
12707 link->link.fd = map->fd;
12712 fd = bpf_link_create(map->fd, 0, BPF_STRUCT_OPS, NULL);
12719 link->map_fd = map->fd;
12725 * Swap the back struct_ops of a link with a new struct_ops map.
12727 int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map)
12733 if (!bpf_map__is_struct_ops(map) || map->fd < 0)
12741 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0);
12742 /* It can be EBUSY if the map has been used to create or
12750 err = bpf_link_update(link->fd, map->fd, NULL);
12754 st_ops_link->map_fd = map->fd;
12847 int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
13004 struct bpf_map_info map;
13018 memset(&map, 0, sizeof(map));
13019 map_info_len = sizeof(map);
13020 err = bpf_map_get_info_by_fd(map_fd, &map, &map_info_len);
13027 pr_warn("failed to get map info for map FD %d: %s\n",
13031 pr_debug("failed to get map info for FD %d; API not supported? Ignoring...\n",
13034 if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
13035 pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
13036 map.name);
13070 if (map.max_entries && map.max_entries < pb->cpu_cnt)
13071 pb->cpu_cnt = map.max_entries;
13233 /* Return number of PERF_EVENT_ARRAY map slots set up by this perf_buffer
13243 * PERF_EVENT_ARRAY BPF map. This FD can be polled for new data using
13278 * PERF_EVENT_ARRAY BPF map without waiting/polling. If there is no data to
13473 struct bpf_map **map = maps[i].map;
13477 *map = bpf_object__find_map_by_name(obj, name);
13478 if (!*map) {
13479 pr_warn("failed to find skeleton map '%s'\n", name);
13484 if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG)
13485 *mmaped = (*map)->mmaped;
13521 * prefix as their own map name prefix. When skeleton is generated,
13558 const struct bpf_map *map;
13589 map = *var_skel->map;
13590 map_type_id = bpf_map__btf_value_type_id(map);
13594 pr_warn("type for map '%1$s' is not a datasec: %2$s",
13595 bpf_map__name(map),
13606 *var_skel->addr = map->mmaped + var->offset;
13635 struct bpf_map *map = *s->maps[i].map;
13636 size_t mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
13637 int prot, map_fd = bpf_map__fd(map);
13643 if (!(map->def.map_flags & BPF_F_MMAPABLE)) {
13648 if (map->def.map_flags & BPF_F_RDONLY_PROG)
13653 /* Remap anonymous mmap()-ed "map initialization image" as
13654 * a BPF map-backed mmap()-ed memory, but preserving the same
13663 *mmaped = mmap(map->mmaped, mmap_sz, prot, MAP_SHARED | MAP_FIXED, map_fd, 0);
13667 pr_warn("failed to re-mmap() map '%s': %d\n",
13668 bpf_map__name(map), err);