@@ -670,11 +670,18 @@ struct elf_state {
670
670
671
671
struct usdt_manager ;
672
672
673
+ enum bpf_object_state {
674
+ OBJ_OPEN ,
675
+ OBJ_PREPARED ,
676
+ OBJ_LOADED ,
677
+ };
678
+
673
679
struct bpf_object {
674
680
char name [BPF_OBJ_NAME_LEN ];
675
681
char license [64 ];
676
682
__u32 kern_version ;
677
683
684
+ enum bpf_object_state state ;
678
685
struct bpf_program * programs ;
679
686
size_t nr_programs ;
680
687
struct bpf_map * maps ;
@@ -686,7 +693,6 @@ struct bpf_object {
686
693
int nr_extern ;
687
694
int kconfig_map_idx ;
688
695
689
- bool loaded ;
690
696
bool has_subcalls ;
691
697
bool has_rodata ;
692
698
@@ -1511,7 +1517,7 @@ static struct bpf_object *bpf_object__new(const char *path,
1511
1517
obj -> kconfig_map_idx = -1 ;
1512
1518
1513
1519
obj -> kern_version = get_kernel_version ();
1514
- obj -> loaded = false ;
1520
+ obj -> state = OBJ_OPEN ;
1515
1521
1516
1522
return obj ;
1517
1523
}
@@ -4847,7 +4853,7 @@ static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info)
4847
4853
4848
4854
static bool map_is_created (const struct bpf_map * map )
4849
4855
{
4850
- return map -> obj -> loaded || map -> reused ;
4856
+ return map -> obj -> state >= OBJ_PREPARED || map -> reused ;
4851
4857
}
4852
4858
4853
4859
bool bpf_map__autocreate (const struct bpf_map * map )
@@ -8550,7 +8556,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
8550
8556
if (!obj )
8551
8557
return libbpf_err (- EINVAL );
8552
8558
8553
- if (obj -> loaded ) {
8559
+ if (obj -> state >= OBJ_LOADED ) {
8554
8560
pr_warn ("object '%s': load can't be attempted twice\n" , obj -> name );
8555
8561
return libbpf_err (- EINVAL );
8556
8562
}
@@ -8602,8 +8608,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
8602
8608
btf__free (obj -> btf_vmlinux );
8603
8609
obj -> btf_vmlinux = NULL ;
8604
8610
8605
- obj -> loaded = true; /* doesn't matter if successfully or not */
8606
-
8611
+ obj -> state = OBJ_LOADED ; /* doesn't matter if successfully or not */
8607
8612
if (err )
8608
8613
goto out ;
8609
8614
@@ -8866,7 +8871,7 @@ int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
8866
8871
if (!obj )
8867
8872
return libbpf_err (- ENOENT );
8868
8873
8869
- if (! obj -> loaded ) {
8874
+ if (obj -> state < OBJ_PREPARED ) {
8870
8875
pr_warn ("object not yet loaded; load it first\n" );
8871
8876
return libbpf_err (- ENOENT );
8872
8877
}
@@ -8945,7 +8950,7 @@ int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
8945
8950
if (!obj )
8946
8951
return libbpf_err (- ENOENT );
8947
8952
8948
- if (! obj -> loaded ) {
8953
+ if (obj -> state < OBJ_LOADED ) {
8949
8954
pr_warn ("object not yet loaded; load it first\n" );
8950
8955
return libbpf_err (- ENOENT );
8951
8956
}
@@ -9132,7 +9137,7 @@ int bpf_object__btf_fd(const struct bpf_object *obj)
9132
9137
9133
9138
int bpf_object__set_kversion (struct bpf_object * obj , __u32 kern_version )
9134
9139
{
9135
- if (obj -> loaded )
9140
+ if (obj -> state >= OBJ_LOADED )
9136
9141
return libbpf_err (- EINVAL );
9137
9142
9138
9143
obj -> kern_version = kern_version ;
@@ -9229,7 +9234,7 @@ bool bpf_program__autoload(const struct bpf_program *prog)
9229
9234
9230
9235
int bpf_program__set_autoload (struct bpf_program * prog , bool autoload )
9231
9236
{
9232
- if (prog -> obj -> loaded )
9237
+ if (prog -> obj -> state >= OBJ_LOADED )
9233
9238
return libbpf_err (- EINVAL );
9234
9239
9235
9240
prog -> autoload = autoload ;
@@ -9261,7 +9266,7 @@ int bpf_program__set_insns(struct bpf_program *prog,
9261
9266
{
9262
9267
struct bpf_insn * insns ;
9263
9268
9264
- if (prog -> obj -> loaded )
9269
+ if (prog -> obj -> state >= OBJ_LOADED )
9265
9270
return libbpf_err (- EBUSY );
9266
9271
9267
9272
insns = libbpf_reallocarray (prog -> insns , new_insn_cnt , sizeof (* insns ));
@@ -9304,7 +9309,7 @@ static int last_custom_sec_def_handler_id;
9304
9309
9305
9310
int bpf_program__set_type (struct bpf_program * prog , enum bpf_prog_type type )
9306
9311
{
9307
- if (prog -> obj -> loaded )
9312
+ if (prog -> obj -> state >= OBJ_LOADED )
9308
9313
return libbpf_err (- EBUSY );
9309
9314
9310
9315
/* if type is not changed, do nothing */
@@ -9335,7 +9340,7 @@ enum bpf_attach_type bpf_program__expected_attach_type(const struct bpf_program
9335
9340
int bpf_program__set_expected_attach_type (struct bpf_program * prog ,
9336
9341
enum bpf_attach_type type )
9337
9342
{
9338
- if (prog -> obj -> loaded )
9343
+ if (prog -> obj -> state >= OBJ_LOADED )
9339
9344
return libbpf_err (- EBUSY );
9340
9345
9341
9346
prog -> expected_attach_type = type ;
@@ -9349,7 +9354,7 @@ __u32 bpf_program__flags(const struct bpf_program *prog)
9349
9354
9350
9355
int bpf_program__set_flags (struct bpf_program * prog , __u32 flags )
9351
9356
{
9352
- if (prog -> obj -> loaded )
9357
+ if (prog -> obj -> state >= OBJ_LOADED )
9353
9358
return libbpf_err (- EBUSY );
9354
9359
9355
9360
prog -> prog_flags = flags ;
@@ -9363,7 +9368,7 @@ __u32 bpf_program__log_level(const struct bpf_program *prog)
9363
9368
9364
9369
int bpf_program__set_log_level (struct bpf_program * prog , __u32 log_level )
9365
9370
{
9366
- if (prog -> obj -> loaded )
9371
+ if (prog -> obj -> state >= OBJ_LOADED )
9367
9372
return libbpf_err (- EBUSY );
9368
9373
9369
9374
prog -> log_level = log_level ;
@@ -9382,7 +9387,7 @@ int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log
9382
9387
return libbpf_err (- EINVAL );
9383
9388
if (prog -> log_size > UINT_MAX )
9384
9389
return libbpf_err (- EINVAL );
9385
- if (prog -> obj -> loaded )
9390
+ if (prog -> obj -> state >= OBJ_LOADED )
9386
9391
return libbpf_err (- EBUSY );
9387
9392
9388
9393
prog -> log_buf = log_buf ;
@@ -13666,7 +13671,7 @@ int bpf_program__set_attach_target(struct bpf_program *prog,
13666
13671
if (!prog || attach_prog_fd < 0 )
13667
13672
return libbpf_err (- EINVAL );
13668
13673
13669
- if (prog -> obj -> loaded )
13674
+ if (prog -> obj -> state >= OBJ_LOADED )
13670
13675
return libbpf_err (- EINVAL );
13671
13676
13672
13677
if (attach_prog_fd && !attach_func_name ) {
0 commit comments