mirror of
https://github.com/torvalds/linux.git
synced 2025-04-09 14:45:27 +00:00
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>
This commit is contained in:
parent
6ef78c4191
commit
9a9e347835
@ -670,11 +670,18 @@ struct elf_state {
|
||||
|
||||
struct usdt_manager;
|
||||
|
||||
enum bpf_object_state {
|
||||
OBJ_OPEN,
|
||||
OBJ_PREPARED,
|
||||
OBJ_LOADED,
|
||||
};
|
||||
|
||||
struct bpf_object {
|
||||
char name[BPF_OBJ_NAME_LEN];
|
||||
char license[64];
|
||||
__u32 kern_version;
|
||||
|
||||
enum bpf_object_state state;
|
||||
struct bpf_program *programs;
|
||||
size_t nr_programs;
|
||||
struct bpf_map *maps;
|
||||
@ -686,7 +693,6 @@ struct bpf_object {
|
||||
int nr_extern;
|
||||
int kconfig_map_idx;
|
||||
|
||||
bool loaded;
|
||||
bool has_subcalls;
|
||||
bool has_rodata;
|
||||
|
||||
@ -1511,7 +1517,7 @@ static struct bpf_object *bpf_object__new(const char *path,
|
||||
obj->kconfig_map_idx = -1;
|
||||
|
||||
obj->kern_version = get_kernel_version();
|
||||
obj->loaded = false;
|
||||
obj->state = OBJ_OPEN;
|
||||
|
||||
return obj;
|
||||
}
|
||||
@ -4847,7 +4853,7 @@ static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info)
|
||||
|
||||
static bool map_is_created(const struct bpf_map *map)
|
||||
{
|
||||
return map->obj->loaded || map->reused;
|
||||
return map->obj->state >= OBJ_PREPARED || map->reused;
|
||||
}
|
||||
|
||||
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
|
||||
if (!obj)
|
||||
return libbpf_err(-EINVAL);
|
||||
|
||||
if (obj->loaded) {
|
||||
if (obj->state >= OBJ_LOADED) {
|
||||
pr_warn("object '%s': load can't be attempted twice\n", obj->name);
|
||||
return libbpf_err(-EINVAL);
|
||||
}
|
||||
@ -8602,8 +8608,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
|
||||
btf__free(obj->btf_vmlinux);
|
||||
obj->btf_vmlinux = NULL;
|
||||
|
||||
obj->loaded = true; /* doesn't matter if successfully or not */
|
||||
|
||||
obj->state = OBJ_LOADED; /* doesn't matter if successfully or not */
|
||||
if (err)
|
||||
goto out;
|
||||
|
||||
@ -8866,7 +8871,7 @@ int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
|
||||
if (!obj)
|
||||
return libbpf_err(-ENOENT);
|
||||
|
||||
if (!obj->loaded) {
|
||||
if (obj->state < OBJ_PREPARED) {
|
||||
pr_warn("object not yet loaded; load it first\n");
|
||||
return libbpf_err(-ENOENT);
|
||||
}
|
||||
@ -8945,7 +8950,7 @@ int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
|
||||
if (!obj)
|
||||
return libbpf_err(-ENOENT);
|
||||
|
||||
if (!obj->loaded) {
|
||||
if (obj->state < OBJ_LOADED) {
|
||||
pr_warn("object not yet loaded; load it first\n");
|
||||
return libbpf_err(-ENOENT);
|
||||
}
|
||||
@ -9132,7 +9137,7 @@ int bpf_object__btf_fd(const struct bpf_object *obj)
|
||||
|
||||
int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version)
|
||||
{
|
||||
if (obj->loaded)
|
||||
if (obj->state >= OBJ_LOADED)
|
||||
return libbpf_err(-EINVAL);
|
||||
|
||||
obj->kern_version = kern_version;
|
||||
@ -9229,7 +9234,7 @@ bool bpf_program__autoload(const struct bpf_program *prog)
|
||||
|
||||
int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)
|
||||
{
|
||||
if (prog->obj->loaded)
|
||||
if (prog->obj->state >= OBJ_LOADED)
|
||||
return libbpf_err(-EINVAL);
|
||||
|
||||
prog->autoload = autoload;
|
||||
@ -9261,7 +9266,7 @@ int bpf_program__set_insns(struct bpf_program *prog,
|
||||
{
|
||||
struct bpf_insn *insns;
|
||||
|
||||
if (prog->obj->loaded)
|
||||
if (prog->obj->state >= OBJ_LOADED)
|
||||
return libbpf_err(-EBUSY);
|
||||
|
||||
insns = libbpf_reallocarray(prog->insns, new_insn_cnt, sizeof(*insns));
|
||||
@ -9304,7 +9309,7 @@ static int last_custom_sec_def_handler_id;
|
||||
|
||||
int bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
|
||||
{
|
||||
if (prog->obj->loaded)
|
||||
if (prog->obj->state >= OBJ_LOADED)
|
||||
return libbpf_err(-EBUSY);
|
||||
|
||||
/* if type is not changed, do nothing */
|
||||
@ -9335,7 +9340,7 @@ enum bpf_attach_type bpf_program__expected_attach_type(const struct bpf_program
|
||||
int bpf_program__set_expected_attach_type(struct bpf_program *prog,
|
||||
enum bpf_attach_type type)
|
||||
{
|
||||
if (prog->obj->loaded)
|
||||
if (prog->obj->state >= OBJ_LOADED)
|
||||
return libbpf_err(-EBUSY);
|
||||
|
||||
prog->expected_attach_type = type;
|
||||
@ -9349,7 +9354,7 @@ __u32 bpf_program__flags(const struct bpf_program *prog)
|
||||
|
||||
int bpf_program__set_flags(struct bpf_program *prog, __u32 flags)
|
||||
{
|
||||
if (prog->obj->loaded)
|
||||
if (prog->obj->state >= OBJ_LOADED)
|
||||
return libbpf_err(-EBUSY);
|
||||
|
||||
prog->prog_flags = flags;
|
||||
@ -9363,7 +9368,7 @@ __u32 bpf_program__log_level(const struct bpf_program *prog)
|
||||
|
||||
int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level)
|
||||
{
|
||||
if (prog->obj->loaded)
|
||||
if (prog->obj->state >= OBJ_LOADED)
|
||||
return libbpf_err(-EBUSY);
|
||||
|
||||
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
|
||||
return libbpf_err(-EINVAL);
|
||||
if (prog->log_size > UINT_MAX)
|
||||
return libbpf_err(-EINVAL);
|
||||
if (prog->obj->loaded)
|
||||
if (prog->obj->state >= OBJ_LOADED)
|
||||
return libbpf_err(-EBUSY);
|
||||
|
||||
prog->log_buf = log_buf;
|
||||
@ -13666,7 +13671,7 @@ int bpf_program__set_attach_target(struct bpf_program *prog,
|
||||
if (!prog || attach_prog_fd < 0)
|
||||
return libbpf_err(-EINVAL);
|
||||
|
||||
if (prog->obj->loaded)
|
||||
if (prog->obj->state >= OBJ_LOADED)
|
||||
return libbpf_err(-EINVAL);
|
||||
|
||||
if (attach_prog_fd && !attach_func_name) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user