Skip to content

Commit f08c18e

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
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: Jiri Olsa <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent fa98b54 commit f08c18e

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5200,6 +5200,11 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
52005200

52015201
static void bpf_map__destroy(struct bpf_map *map);
52025202

5203+
static bool map_is_created(const struct bpf_map *map)
5204+
{
5205+
return map->obj->loaded || map->reused;
5206+
}
5207+
52035208
static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner)
52045209
{
52055210
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
52315236
map->name, err);
52325237
return err;
52335238
}
5234-
map->inner_map_fd = bpf_map__fd(map->inner_map);
5239+
map->inner_map_fd = map->inner_map->fd;
52355240
}
52365241
if (map->inner_map_fd >= 0)
52375242
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)
53145319
continue;
53155320

53165321
targ_map = map->init_slots[i];
5317-
fd = bpf_map__fd(targ_map);
5322+
fd = targ_map->fd;
53185323

53195324
if (obj->gen_loader) {
53205325
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
71357140
if (map->libbpf_type != LIBBPF_MAP_RODATA)
71367141
continue;
71377142

7138-
if (bpf_prog_bind_map(ret, bpf_map__fd(map), NULL)) {
7143+
if (bpf_prog_bind_map(ret, map->fd, NULL)) {
71397144
cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
71407145
pr_warn("prog '%s': failed to bind map '%s': %s\n",
71417146
prog->name, map->real_name, cp);
@@ -9601,7 +9606,11 @@ int libbpf_attach_type_by_name(const char *name,
96019606

96029607
int bpf_map__fd(const struct bpf_map *map)
96039608
{
9604-
return map ? map->fd : libbpf_err(-EINVAL);
9609+
if (!map)
9610+
return libbpf_err(-EINVAL);
9611+
if (!map_is_created(map))
9612+
return -1;
9613+
return map->fd;
96059614
}
96069615

96079616
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)
96379646

96389647
int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type)
96399648
{
9640-
if (map->fd >= 0)
9649+
if (map_is_created(map))
96419650
return libbpf_err(-EBUSY);
96429651
map->def.type = type;
96439652
return 0;
@@ -9650,7 +9659,7 @@ __u32 bpf_map__map_flags(const struct bpf_map *map)
96509659

96519660
int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags)
96529661
{
9653-
if (map->fd >= 0)
9662+
if (map_is_created(map))
96549663
return libbpf_err(-EBUSY);
96559664
map->def.map_flags = flags;
96569665
return 0;
@@ -9663,7 +9672,7 @@ __u64 bpf_map__map_extra(const struct bpf_map *map)
96639672

96649673
int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra)
96659674
{
9666-
if (map->fd >= 0)
9675+
if (map_is_created(map))
96679676
return libbpf_err(-EBUSY);
96689677
map->map_extra = map_extra;
96699678
return 0;
@@ -9676,7 +9685,7 @@ __u32 bpf_map__numa_node(const struct bpf_map *map)
96769685

96779686
int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node)
96789687
{
9679-
if (map->fd >= 0)
9688+
if (map_is_created(map))
96809689
return libbpf_err(-EBUSY);
96819690
map->numa_node = numa_node;
96829691
return 0;
@@ -9689,7 +9698,7 @@ __u32 bpf_map__key_size(const struct bpf_map *map)
96899698

96909699
int bpf_map__set_key_size(struct bpf_map *map, __u32 size)
96919700
{
9692-
if (map->fd >= 0)
9701+
if (map_is_created(map))
96939702
return libbpf_err(-EBUSY);
96949703
map->def.key_size = size;
96959704
return 0;
@@ -9773,7 +9782,7 @@ static int map_btf_datasec_resize(struct bpf_map *map, __u32 size)
97739782

97749783
int bpf_map__set_value_size(struct bpf_map *map, __u32 size)
97759784
{
9776-
if (map->fd >= 0)
9785+
if (map->obj->loaded || map->reused)
97779786
return libbpf_err(-EBUSY);
97789787

97799788
if (map->mmaped) {
@@ -9814,8 +9823,11 @@ __u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
98149823
int bpf_map__set_initial_value(struct bpf_map *map,
98159824
const void *data, size_t size)
98169825
{
9826+
if (map->obj->loaded || map->reused)
9827+
return libbpf_err(-EBUSY);
9828+
98179829
if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG ||
9818-
size != map->def.value_size || map->fd >= 0)
9830+
size != map->def.value_size)
98199831
return libbpf_err(-EINVAL);
98209832

98219833
memcpy(map->mmaped, data, size);
@@ -9842,7 +9854,7 @@ __u32 bpf_map__ifindex(const struct bpf_map *map)
98429854

98439855
int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
98449856
{
9845-
if (map->fd >= 0)
9857+
if (map_is_created(map))
98469858
return libbpf_err(-EBUSY);
98479859
map->map_ifindex = ifindex;
98489860
return 0;
@@ -9947,7 +9959,7 @@ bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
99479959
static int validate_map_op(const struct bpf_map *map, size_t key_sz,
99489960
size_t value_sz, bool check_value_sz)
99499961
{
9950-
if (map->fd <= 0)
9962+
if (!map_is_created(map)) /* map is not yet created */
99519963
return -ENOENT;
99529964

99539965
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)
1240012412
__u32 zero = 0;
1240112413
int err;
1240212414

12403-
if (!bpf_map__is_struct_ops(map) || map->fd < 0)
12415+
if (!bpf_map__is_struct_ops(map) || !map_is_created(map))
1240412416
return -EINVAL;
1240512417

1240612418
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)
1330413316
for (i = 0; i < s->map_cnt; i++) {
1330513317
struct bpf_map *map = *s->maps[i].map;
1330613318
size_t mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
13307-
int prot, map_fd = bpf_map__fd(map);
13319+
int prot, map_fd = map->fd;
1330813320
void **mmaped = s->maps[i].mmaped;
1330913321

1331013322
if (!mmaped)

0 commit comments

Comments
 (0)