Commit f08c18e0 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

libbpf: don't rely on map->fd as an indicator of map being created

With the upcoming switch to preallocated placeholder FDs for maps,
switch various getters/setter away from checking map->fd. Use
map_is_created() helper that detect whether BPF map can be modified based
on map->obj->loaded state, with special provision for maps set up with
bpf_map__reuse_fd().

For backwards compatibility, we take map_is_created() into account in
bpf_map__fd() getter as well. This way before bpf_object__load() phase
bpf_map__fd() will always return -1, just as before the changes in
subsequent patches adding stable map->fd placeholders.

We also get rid of all internal uses of bpf_map__fd() getter, as it's
more oriented for uses external to libbpf. The above map_is_created()
check actually interferes with some of the internal uses, if map FD is
fetched through bpf_map__fd().
Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20240104013847.3875810-4-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent fa98b54b
...@@ -5200,6 +5200,11 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map) ...@@ -5200,6 +5200,11 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
static void bpf_map__destroy(struct bpf_map *map); static void bpf_map__destroy(struct bpf_map *map);
static bool map_is_created(const struct bpf_map *map)
{
return map->obj->loaded || map->reused;
}
static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner) static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner)
{ {
LIBBPF_OPTS(bpf_map_create_opts, create_attr); LIBBPF_OPTS(bpf_map_create_opts, create_attr);
...@@ -5231,7 +5236,7 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b ...@@ -5231,7 +5236,7 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b
map->name, err); map->name, err);
return err; return err;
} }
map->inner_map_fd = bpf_map__fd(map->inner_map); map->inner_map_fd = map->inner_map->fd;
} }
if (map->inner_map_fd >= 0) if (map->inner_map_fd >= 0)
create_attr.inner_map_fd = map->inner_map_fd; create_attr.inner_map_fd = map->inner_map_fd;
...@@ -5314,7 +5319,7 @@ static int init_map_in_map_slots(struct bpf_object *obj, struct bpf_map *map) ...@@ -5314,7 +5319,7 @@ static int init_map_in_map_slots(struct bpf_object *obj, struct bpf_map *map)
continue; continue;
targ_map = map->init_slots[i]; targ_map = map->init_slots[i];
fd = bpf_map__fd(targ_map); fd = targ_map->fd;
if (obj->gen_loader) { if (obj->gen_loader) {
bpf_gen__populate_outer_map(obj->gen_loader, bpf_gen__populate_outer_map(obj->gen_loader,
...@@ -7135,7 +7140,7 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog ...@@ -7135,7 +7140,7 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
if (map->libbpf_type != LIBBPF_MAP_RODATA) if (map->libbpf_type != LIBBPF_MAP_RODATA)
continue; continue;
if (bpf_prog_bind_map(ret, bpf_map__fd(map), NULL)) { if (bpf_prog_bind_map(ret, map->fd, NULL)) {
cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
pr_warn("prog '%s': failed to bind map '%s': %s\n", pr_warn("prog '%s': failed to bind map '%s': %s\n",
prog->name, map->real_name, cp); prog->name, map->real_name, cp);
...@@ -9601,7 +9606,11 @@ int libbpf_attach_type_by_name(const char *name, ...@@ -9601,7 +9606,11 @@ int libbpf_attach_type_by_name(const char *name,
int bpf_map__fd(const struct bpf_map *map) int bpf_map__fd(const struct bpf_map *map)
{ {
return map ? map->fd : libbpf_err(-EINVAL); if (!map)
return libbpf_err(-EINVAL);
if (!map_is_created(map))
return -1;
return map->fd;
} }
static bool map_uses_real_name(const struct bpf_map *map) static bool map_uses_real_name(const struct bpf_map *map)
...@@ -9637,7 +9646,7 @@ enum bpf_map_type bpf_map__type(const struct bpf_map *map) ...@@ -9637,7 +9646,7 @@ enum bpf_map_type bpf_map__type(const struct bpf_map *map)
int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type) int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type)
{ {
if (map->fd >= 0) if (map_is_created(map))
return libbpf_err(-EBUSY); return libbpf_err(-EBUSY);
map->def.type = type; map->def.type = type;
return 0; return 0;
...@@ -9650,7 +9659,7 @@ __u32 bpf_map__map_flags(const struct bpf_map *map) ...@@ -9650,7 +9659,7 @@ __u32 bpf_map__map_flags(const struct bpf_map *map)
int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags) int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags)
{ {
if (map->fd >= 0) if (map_is_created(map))
return libbpf_err(-EBUSY); return libbpf_err(-EBUSY);
map->def.map_flags = flags; map->def.map_flags = flags;
return 0; return 0;
...@@ -9663,7 +9672,7 @@ __u64 bpf_map__map_extra(const struct bpf_map *map) ...@@ -9663,7 +9672,7 @@ __u64 bpf_map__map_extra(const struct bpf_map *map)
int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra) int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra)
{ {
if (map->fd >= 0) if (map_is_created(map))
return libbpf_err(-EBUSY); return libbpf_err(-EBUSY);
map->map_extra = map_extra; map->map_extra = map_extra;
return 0; return 0;
...@@ -9676,7 +9685,7 @@ __u32 bpf_map__numa_node(const struct bpf_map *map) ...@@ -9676,7 +9685,7 @@ __u32 bpf_map__numa_node(const struct bpf_map *map)
int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node) int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node)
{ {
if (map->fd >= 0) if (map_is_created(map))
return libbpf_err(-EBUSY); return libbpf_err(-EBUSY);
map->numa_node = numa_node; map->numa_node = numa_node;
return 0; return 0;
...@@ -9689,7 +9698,7 @@ __u32 bpf_map__key_size(const struct bpf_map *map) ...@@ -9689,7 +9698,7 @@ __u32 bpf_map__key_size(const struct bpf_map *map)
int bpf_map__set_key_size(struct bpf_map *map, __u32 size) int bpf_map__set_key_size(struct bpf_map *map, __u32 size)
{ {
if (map->fd >= 0) if (map_is_created(map))
return libbpf_err(-EBUSY); return libbpf_err(-EBUSY);
map->def.key_size = size; map->def.key_size = size;
return 0; return 0;
...@@ -9773,7 +9782,7 @@ static int map_btf_datasec_resize(struct bpf_map *map, __u32 size) ...@@ -9773,7 +9782,7 @@ static int map_btf_datasec_resize(struct bpf_map *map, __u32 size)
int bpf_map__set_value_size(struct bpf_map *map, __u32 size) int bpf_map__set_value_size(struct bpf_map *map, __u32 size)
{ {
if (map->fd >= 0) if (map->obj->loaded || map->reused)
return libbpf_err(-EBUSY); return libbpf_err(-EBUSY);
if (map->mmaped) { if (map->mmaped) {
...@@ -9814,8 +9823,11 @@ __u32 bpf_map__btf_value_type_id(const struct bpf_map *map) ...@@ -9814,8 +9823,11 @@ __u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
int bpf_map__set_initial_value(struct bpf_map *map, int bpf_map__set_initial_value(struct bpf_map *map,
const void *data, size_t size) const void *data, size_t size)
{ {
if (map->obj->loaded || map->reused)
return libbpf_err(-EBUSY);
if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG || if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG ||
size != map->def.value_size || map->fd >= 0) size != map->def.value_size)
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
memcpy(map->mmaped, data, size); memcpy(map->mmaped, data, size);
...@@ -9842,7 +9854,7 @@ __u32 bpf_map__ifindex(const struct bpf_map *map) ...@@ -9842,7 +9854,7 @@ __u32 bpf_map__ifindex(const struct bpf_map *map)
int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex) int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
{ {
if (map->fd >= 0) if (map_is_created(map))
return libbpf_err(-EBUSY); return libbpf_err(-EBUSY);
map->map_ifindex = ifindex; map->map_ifindex = ifindex;
return 0; return 0;
...@@ -9947,7 +9959,7 @@ bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name) ...@@ -9947,7 +9959,7 @@ bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
static int validate_map_op(const struct bpf_map *map, size_t key_sz, static int validate_map_op(const struct bpf_map *map, size_t key_sz,
size_t value_sz, bool check_value_sz) size_t value_sz, bool check_value_sz)
{ {
if (map->fd <= 0) if (!map_is_created(map)) /* map is not yet created */
return -ENOENT; return -ENOENT;
if (map->def.key_size != key_sz) { if (map->def.key_size != key_sz) {
...@@ -12400,7 +12412,7 @@ int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map) ...@@ -12400,7 +12412,7 @@ int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map)
__u32 zero = 0; __u32 zero = 0;
int err; int err;
if (!bpf_map__is_struct_ops(map) || map->fd < 0) if (!bpf_map__is_struct_ops(map) || !map_is_created(map))
return -EINVAL; return -EINVAL;
st_ops_link = container_of(link, struct bpf_link_struct_ops, link); st_ops_link = container_of(link, struct bpf_link_struct_ops, link);
...@@ -13304,7 +13316,7 @@ int bpf_object__load_skeleton(struct bpf_object_skeleton *s) ...@@ -13304,7 +13316,7 @@ int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
for (i = 0; i < s->map_cnt; i++) { for (i = 0; i < s->map_cnt; i++) {
struct bpf_map *map = *s->maps[i].map; struct bpf_map *map = *s->maps[i].map;
size_t mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries); size_t mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
int prot, map_fd = bpf_map__fd(map); int prot, map_fd = map->fd;
void **mmaped = s->maps[i].mmaped; void **mmaped = s->maps[i].mmaped;
if (!mmaped) if (!mmaped)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment