Skip to content

Commit 9a9e347

Browse files
mykyta5Alexei Starovoitov
authored andcommitted
libbpf: Introduce more granular state for bpf_object
We are going to split bpf_object loading into 2 stages: preparation and loading. This will increase flexibility when working with bpf_object and unlock some optimizations and use cases. This patch substitutes a boolean flag (loaded) by more finely-grained state for bpf_object. Signed-off-by: Mykyta Yatsenko <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 6ef78c4 commit 9a9e347

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -670,11 +670,18 @@ struct elf_state {
670670

671671
struct usdt_manager;
672672

673+
enum bpf_object_state {
674+
OBJ_OPEN,
675+
OBJ_PREPARED,
676+
OBJ_LOADED,
677+
};
678+
673679
struct bpf_object {
674680
char name[BPF_OBJ_NAME_LEN];
675681
char license[64];
676682
__u32 kern_version;
677683

684+
enum bpf_object_state state;
678685
struct bpf_program *programs;
679686
size_t nr_programs;
680687
struct bpf_map *maps;
@@ -686,7 +693,6 @@ struct bpf_object {
686693
int nr_extern;
687694
int kconfig_map_idx;
688695

689-
bool loaded;
690696
bool has_subcalls;
691697
bool has_rodata;
692698

@@ -1511,7 +1517,7 @@ static struct bpf_object *bpf_object__new(const char *path,
15111517
obj->kconfig_map_idx = -1;
15121518

15131519
obj->kern_version = get_kernel_version();
1514-
obj->loaded = false;
1520+
obj->state = OBJ_OPEN;
15151521

15161522
return obj;
15171523
}
@@ -4847,7 +4853,7 @@ static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info)
48474853

48484854
static bool map_is_created(const struct bpf_map *map)
48494855
{
4850-
return map->obj->loaded || map->reused;
4856+
return map->obj->state >= OBJ_PREPARED || map->reused;
48514857
}
48524858

48534859
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
85508556
if (!obj)
85518557
return libbpf_err(-EINVAL);
85528558

8553-
if (obj->loaded) {
8559+
if (obj->state >= OBJ_LOADED) {
85548560
pr_warn("object '%s': load can't be attempted twice\n", obj->name);
85558561
return libbpf_err(-EINVAL);
85568562
}
@@ -8602,8 +8608,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
86028608
btf__free(obj->btf_vmlinux);
86038609
obj->btf_vmlinux = NULL;
86048610

8605-
obj->loaded = true; /* doesn't matter if successfully or not */
8606-
8611+
obj->state = OBJ_LOADED; /* doesn't matter if successfully or not */
86078612
if (err)
86088613
goto out;
86098614

@@ -8866,7 +8871,7 @@ int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
88668871
if (!obj)
88678872
return libbpf_err(-ENOENT);
88688873

8869-
if (!obj->loaded) {
8874+
if (obj->state < OBJ_PREPARED) {
88708875
pr_warn("object not yet loaded; load it first\n");
88718876
return libbpf_err(-ENOENT);
88728877
}
@@ -8945,7 +8950,7 @@ int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
89458950
if (!obj)
89468951
return libbpf_err(-ENOENT);
89478952

8948-
if (!obj->loaded) {
8953+
if (obj->state < OBJ_LOADED) {
89498954
pr_warn("object not yet loaded; load it first\n");
89508955
return libbpf_err(-ENOENT);
89518956
}
@@ -9132,7 +9137,7 @@ int bpf_object__btf_fd(const struct bpf_object *obj)
91329137

91339138
int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version)
91349139
{
9135-
if (obj->loaded)
9140+
if (obj->state >= OBJ_LOADED)
91369141
return libbpf_err(-EINVAL);
91379142

91389143
obj->kern_version = kern_version;
@@ -9229,7 +9234,7 @@ bool bpf_program__autoload(const struct bpf_program *prog)
92299234

92309235
int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)
92319236
{
9232-
if (prog->obj->loaded)
9237+
if (prog->obj->state >= OBJ_LOADED)
92339238
return libbpf_err(-EINVAL);
92349239

92359240
prog->autoload = autoload;
@@ -9261,7 +9266,7 @@ int bpf_program__set_insns(struct bpf_program *prog,
92619266
{
92629267
struct bpf_insn *insns;
92639268

9264-
if (prog->obj->loaded)
9269+
if (prog->obj->state >= OBJ_LOADED)
92659270
return libbpf_err(-EBUSY);
92669271

92679272
insns = libbpf_reallocarray(prog->insns, new_insn_cnt, sizeof(*insns));
@@ -9304,7 +9309,7 @@ static int last_custom_sec_def_handler_id;
93049309

93059310
int bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
93069311
{
9307-
if (prog->obj->loaded)
9312+
if (prog->obj->state >= OBJ_LOADED)
93089313
return libbpf_err(-EBUSY);
93099314

93109315
/* if type is not changed, do nothing */
@@ -9335,7 +9340,7 @@ enum bpf_attach_type bpf_program__expected_attach_type(const struct bpf_program
93359340
int bpf_program__set_expected_attach_type(struct bpf_program *prog,
93369341
enum bpf_attach_type type)
93379342
{
9338-
if (prog->obj->loaded)
9343+
if (prog->obj->state >= OBJ_LOADED)
93399344
return libbpf_err(-EBUSY);
93409345

93419346
prog->expected_attach_type = type;
@@ -9349,7 +9354,7 @@ __u32 bpf_program__flags(const struct bpf_program *prog)
93499354

93509355
int bpf_program__set_flags(struct bpf_program *prog, __u32 flags)
93519356
{
9352-
if (prog->obj->loaded)
9357+
if (prog->obj->state >= OBJ_LOADED)
93539358
return libbpf_err(-EBUSY);
93549359

93559360
prog->prog_flags = flags;
@@ -9363,7 +9368,7 @@ __u32 bpf_program__log_level(const struct bpf_program *prog)
93639368

93649369
int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level)
93659370
{
9366-
if (prog->obj->loaded)
9371+
if (prog->obj->state >= OBJ_LOADED)
93679372
return libbpf_err(-EBUSY);
93689373

93699374
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
93829387
return libbpf_err(-EINVAL);
93839388
if (prog->log_size > UINT_MAX)
93849389
return libbpf_err(-EINVAL);
9385-
if (prog->obj->loaded)
9390+
if (prog->obj->state >= OBJ_LOADED)
93869391
return libbpf_err(-EBUSY);
93879392

93889393
prog->log_buf = log_buf;
@@ -13666,7 +13671,7 @@ int bpf_program__set_attach_target(struct bpf_program *prog,
1366613671
if (!prog || attach_prog_fd < 0)
1366713672
return libbpf_err(-EINVAL);
1366813673

13669-
if (prog->obj->loaded)
13674+
if (prog->obj->state >= OBJ_LOADED)
1367013675
return libbpf_err(-EINVAL);
1367113676

1367213677
if (attach_prog_fd && !attach_func_name) {

0 commit comments

Comments
 (0)