Skip to content

Commit 235feb2

Browse files
committed
libbpf: Introduce more granular state for bpf_object
JIRA: https://issues.redhat.com/browse/RHEL-78202 commit 9a9e347 Author: Mykyta Yatsenko <yatsenko@meta.com> Date: Mon Mar 3 13:57:50 2025 +0000 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 <yatsenko@meta.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20250303135752.158343-3-mykyta.yatsenko5@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Gregory Bell <grbell@redhat.com>
1 parent 9c6e755 commit 235feb2

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
}
@@ -4838,7 +4844,7 @@ static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info)
48384844

48394845
static bool map_is_created(const struct bpf_map *map)
48404846
{
4841-
return map->obj->loaded || map->reused;
4847+
return map->obj->state >= OBJ_PREPARED || map->reused;
48424848
}
48434849

48444850
bool bpf_map__autocreate(const struct bpf_map *map)
@@ -8541,7 +8547,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
85418547
if (!obj)
85428548
return libbpf_err(-EINVAL);
85438549

8544-
if (obj->loaded) {
8550+
if (obj->state >= OBJ_LOADED) {
85458551
pr_warn("object '%s': load can't be attempted twice\n", obj->name);
85468552
return libbpf_err(-EINVAL);
85478553
}
@@ -8593,8 +8599,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
85938599
btf__free(obj->btf_vmlinux);
85948600
obj->btf_vmlinux = NULL;
85958601

8596-
obj->loaded = true; /* doesn't matter if successfully or not */
8597-
8602+
obj->state = OBJ_LOADED; /* doesn't matter if successfully or not */
85988603
if (err)
85998604
goto out;
86008605

@@ -8857,7 +8862,7 @@ int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
88578862
if (!obj)
88588863
return libbpf_err(-ENOENT);
88598864

8860-
if (!obj->loaded) {
8865+
if (obj->state < OBJ_PREPARED) {
88618866
pr_warn("object not yet loaded; load it first\n");
88628867
return libbpf_err(-ENOENT);
88638868
}
@@ -8936,7 +8941,7 @@ int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
89368941
if (!obj)
89378942
return libbpf_err(-ENOENT);
89388943

8939-
if (!obj->loaded) {
8944+
if (obj->state < OBJ_LOADED) {
89408945
pr_warn("object not yet loaded; load it first\n");
89418946
return libbpf_err(-ENOENT);
89428947
}
@@ -9123,7 +9128,7 @@ int bpf_object__btf_fd(const struct bpf_object *obj)
91239128

91249129
int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version)
91259130
{
9126-
if (obj->loaded)
9131+
if (obj->state >= OBJ_LOADED)
91279132
return libbpf_err(-EINVAL);
91289133

91299134
obj->kern_version = kern_version;
@@ -9220,7 +9225,7 @@ bool bpf_program__autoload(const struct bpf_program *prog)
92209225

92219226
int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)
92229227
{
9223-
if (prog->obj->loaded)
9228+
if (prog->obj->state >= OBJ_LOADED)
92249229
return libbpf_err(-EINVAL);
92259230

92269231
prog->autoload = autoload;
@@ -9252,7 +9257,7 @@ int bpf_program__set_insns(struct bpf_program *prog,
92529257
{
92539258
struct bpf_insn *insns;
92549259

9255-
if (prog->obj->loaded)
9260+
if (prog->obj->state >= OBJ_LOADED)
92569261
return libbpf_err(-EBUSY);
92579262

92589263
insns = libbpf_reallocarray(prog->insns, new_insn_cnt, sizeof(*insns));
@@ -9295,7 +9300,7 @@ static int last_custom_sec_def_handler_id;
92959300

92969301
int bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
92979302
{
9298-
if (prog->obj->loaded)
9303+
if (prog->obj->state >= OBJ_LOADED)
92999304
return libbpf_err(-EBUSY);
93009305

93019306
/* if type is not changed, do nothing */
@@ -9326,7 +9331,7 @@ enum bpf_attach_type bpf_program__expected_attach_type(const struct bpf_program
93269331
int bpf_program__set_expected_attach_type(struct bpf_program *prog,
93279332
enum bpf_attach_type type)
93289333
{
9329-
if (prog->obj->loaded)
9334+
if (prog->obj->state >= OBJ_LOADED)
93309335
return libbpf_err(-EBUSY);
93319336

93329337
prog->expected_attach_type = type;
@@ -9340,7 +9345,7 @@ __u32 bpf_program__flags(const struct bpf_program *prog)
93409345

93419346
int bpf_program__set_flags(struct bpf_program *prog, __u32 flags)
93429347
{
9343-
if (prog->obj->loaded)
9348+
if (prog->obj->state >= OBJ_LOADED)
93449349
return libbpf_err(-EBUSY);
93459350

93469351
prog->prog_flags = flags;
@@ -9354,7 +9359,7 @@ __u32 bpf_program__log_level(const struct bpf_program *prog)
93549359

93559360
int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level)
93569361
{
9357-
if (prog->obj->loaded)
9362+
if (prog->obj->state >= OBJ_LOADED)
93589363
return libbpf_err(-EBUSY);
93599364

93609365
prog->log_level = log_level;
@@ -9373,7 +9378,7 @@ int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log
93739378
return libbpf_err(-EINVAL);
93749379
if (prog->log_size > UINT_MAX)
93759380
return libbpf_err(-EINVAL);
9376-
if (prog->obj->loaded)
9381+
if (prog->obj->state >= OBJ_LOADED)
93779382
return libbpf_err(-EBUSY);
93789383

93799384
prog->log_buf = log_buf;
@@ -13657,7 +13662,7 @@ int bpf_program__set_attach_target(struct bpf_program *prog,
1365713662
if (!prog || attach_prog_fd < 0)
1365813663
return libbpf_err(-EINVAL);
1365913664

13660-
if (prog->obj->loaded)
13665+
if (prog->obj->state >= OBJ_LOADED)
1366113666
return libbpf_err(-EINVAL);
1366213667

1366313668
if (attach_prog_fd && !attach_func_name) {

0 commit comments

Comments
 (0)