@@ -5200,6 +5200,11 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
5200
5200
5201
5201
static void bpf_map__destroy (struct bpf_map * map );
5202
5202
5203
+ static bool map_is_created (const struct bpf_map * map )
5204
+ {
5205
+ return map -> obj -> loaded || map -> reused ;
5206
+ }
5207
+
5203
5208
static int bpf_object__create_map (struct bpf_object * obj , struct bpf_map * map , bool is_inner )
5204
5209
{
5205
5210
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
5236
map -> name , err );
5232
5237
return err ;
5233
5238
}
5234
- map -> inner_map_fd = bpf_map__fd ( map -> inner_map ) ;
5239
+ map -> inner_map_fd = map -> inner_map -> fd ;
5235
5240
}
5236
5241
if (map -> inner_map_fd >= 0 )
5237
5242
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
5319
continue ;
5315
5320
5316
5321
targ_map = map -> init_slots [i ];
5317
- fd = bpf_map__fd ( targ_map ) ;
5322
+ fd = targ_map -> fd ;
5318
5323
5319
5324
if (obj -> gen_loader ) {
5320
5325
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
7140
if (map -> libbpf_type != LIBBPF_MAP_RODATA )
7136
7141
continue ;
7137
7142
7138
- if (bpf_prog_bind_map (ret , bpf_map__fd ( map ) , NULL )) {
7143
+ if (bpf_prog_bind_map (ret , map -> fd , NULL )) {
7139
7144
cp = libbpf_strerror_r (errno , errmsg , sizeof (errmsg ));
7140
7145
pr_warn ("prog '%s': failed to bind map '%s': %s\n" ,
7141
7146
prog -> name , map -> real_name , cp );
@@ -9601,7 +9606,11 @@ int libbpf_attach_type_by_name(const char *name,
9601
9606
9602
9607
int bpf_map__fd (const struct bpf_map * map )
9603
9608
{
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 ;
9605
9614
}
9606
9615
9607
9616
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
9646
9638
9647
int bpf_map__set_type (struct bpf_map * map , enum bpf_map_type type )
9639
9648
{
9640
- if (map -> fd >= 0 )
9649
+ if (map_is_created ( map ) )
9641
9650
return libbpf_err (- EBUSY );
9642
9651
map -> def .type = type ;
9643
9652
return 0 ;
@@ -9650,7 +9659,7 @@ __u32 bpf_map__map_flags(const struct bpf_map *map)
9650
9659
9651
9660
int bpf_map__set_map_flags (struct bpf_map * map , __u32 flags )
9652
9661
{
9653
- if (map -> fd >= 0 )
9662
+ if (map_is_created ( map ) )
9654
9663
return libbpf_err (- EBUSY );
9655
9664
map -> def .map_flags = flags ;
9656
9665
return 0 ;
@@ -9663,7 +9672,7 @@ __u64 bpf_map__map_extra(const struct bpf_map *map)
9663
9672
9664
9673
int bpf_map__set_map_extra (struct bpf_map * map , __u64 map_extra )
9665
9674
{
9666
- if (map -> fd >= 0 )
9675
+ if (map_is_created ( map ) )
9667
9676
return libbpf_err (- EBUSY );
9668
9677
map -> map_extra = map_extra ;
9669
9678
return 0 ;
@@ -9676,7 +9685,7 @@ __u32 bpf_map__numa_node(const struct bpf_map *map)
9676
9685
9677
9686
int bpf_map__set_numa_node (struct bpf_map * map , __u32 numa_node )
9678
9687
{
9679
- if (map -> fd >= 0 )
9688
+ if (map_is_created ( map ) )
9680
9689
return libbpf_err (- EBUSY );
9681
9690
map -> numa_node = numa_node ;
9682
9691
return 0 ;
@@ -9689,7 +9698,7 @@ __u32 bpf_map__key_size(const struct bpf_map *map)
9689
9698
9690
9699
int bpf_map__set_key_size (struct bpf_map * map , __u32 size )
9691
9700
{
9692
- if (map -> fd >= 0 )
9701
+ if (map_is_created ( map ) )
9693
9702
return libbpf_err (- EBUSY );
9694
9703
map -> def .key_size = size ;
9695
9704
return 0 ;
@@ -9773,7 +9782,7 @@ static int map_btf_datasec_resize(struct bpf_map *map, __u32 size)
9773
9782
9774
9783
int bpf_map__set_value_size (struct bpf_map * map , __u32 size )
9775
9784
{
9776
- if (map -> fd >= 0 )
9785
+ if (map -> obj -> loaded || map -> reused )
9777
9786
return libbpf_err (- EBUSY );
9778
9787
9779
9788
if (map -> mmaped ) {
@@ -9814,8 +9823,11 @@ __u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
9814
9823
int bpf_map__set_initial_value (struct bpf_map * map ,
9815
9824
const void * data , size_t size )
9816
9825
{
9826
+ if (map -> obj -> loaded || map -> reused )
9827
+ return libbpf_err (- EBUSY );
9828
+
9817
9829
if (!map -> mmaped || map -> libbpf_type == LIBBPF_MAP_KCONFIG ||
9818
- size != map -> def .value_size || map -> fd >= 0 )
9830
+ size != map -> def .value_size )
9819
9831
return libbpf_err (- EINVAL );
9820
9832
9821
9833
memcpy (map -> mmaped , data , size );
@@ -9842,7 +9854,7 @@ __u32 bpf_map__ifindex(const struct bpf_map *map)
9842
9854
9843
9855
int bpf_map__set_ifindex (struct bpf_map * map , __u32 ifindex )
9844
9856
{
9845
- if (map -> fd >= 0 )
9857
+ if (map_is_created ( map ) )
9846
9858
return libbpf_err (- EBUSY );
9847
9859
map -> map_ifindex = ifindex ;
9848
9860
return 0 ;
@@ -9947,7 +9959,7 @@ bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
9947
9959
static int validate_map_op (const struct bpf_map * map , size_t key_sz ,
9948
9960
size_t value_sz , bool check_value_sz )
9949
9961
{
9950
- if (map -> fd <= 0 )
9962
+ if (! map_is_created ( map )) /* map is not yet created */
9951
9963
return - ENOENT ;
9952
9964
9953
9965
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
12412
__u32 zero = 0 ;
12401
12413
int err ;
12402
12414
12403
- if (!bpf_map__is_struct_ops (map ) || map -> fd < 0 )
12415
+ if (!bpf_map__is_struct_ops (map ) || ! map_is_created ( map ) )
12404
12416
return - EINVAL ;
12405
12417
12406
12418
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
13316
for (i = 0 ; i < s -> map_cnt ; i ++ ) {
13305
13317
struct bpf_map * map = * s -> maps [i ].map ;
13306
13318
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 ;
13308
13320
void * * mmaped = s -> maps [i ].mmaped ;
13309
13321
13310
13322
if (!mmaped )
0 commit comments