libbpf: deprecate bpf_object__unload() API
PATCH 1/1 libbpf: deprecate bpf_object__unload() API
BPF objects are not re-loadable after unload. User are expected to use bpf_object__close() to unload and free up resources in one operation. No need to expose bpf_object__unload() as a public API, deprecate it.[0] Remove bpf_object__unload() inside bpf_object__load_xattr(), it is the caller's responsibility to free up resources, otherwise, the following code path will cause double-free problem when loading failed:
bpf_prog_load
bpf_prog_load_xattr
bpf_object__load
bpf_object__load_xattr
Replace bpf_object__unload() inside bpf_object__close() with the necessary cleanup operations to avoid compilation error.
[0] Closes: https://github.com/libbpf/libbpf/issues/290
--- tools/lib/bpf/libbpf.c | 8 +++++--- tools/lib/bpf/libbpf.h | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-)
tools/lib/bpf/libbpf.c +5 -3
@@ -6931,7 +6931,6 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)if (obj->maps[i].pinned && !obj->maps[i].reused)bpf_map__unpin (&obj->maps[i],NULL ); -bpf_object__unload (obj);pr_warn ("failed to load object '%s'\n" , obj->path);return libbpf_err (err); }
@@ -7540,12 +7539,15 @@ void bpf_object__close(struct bpf_object *obj)bpf_gen__free (obj->gen_loader);bpf_object__elf_finish (obj); -bpf_object__unload (obj);btf__free (obj->btf);btf_ext__free (obj->btf_ext); -for (i =0 ; i < obj->nr_maps; i++) +for (i =0 ; i < obj->nr_maps; i++) { +zclose (obj->maps[i].fd); +if (obj->maps[i].st_ops) +zfree (&obj->maps[i].st_ops->kern_vdata);bpf_map__destroy (&obj->maps[i]); + }zfree (&obj->btf_custom_path);zfree (&obj->kconfig);
tools/lib/bpf/libbpf.h +2 -1
@@ -147,7 +147,8 @@ struct bpf_object_load_attr {/* Load/unload object into/from kernel */ LIBBPF_API int bpf_object__load(struct bpf_object *obj); LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr); -LIBBPF_API int bpf_object__unload(struct bpf_object *obj); +LIBBPF_API LIBBPF_DEPRECATED("bpf_object__unload() is deprecated, use bpf_object__close() instead") +int bpf_object__unload (struct bpf_object *obj); LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj); LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
Signature · 1 line
2.25.1
Quoted 8 lines · Hengqi Chen
BPF objects are not re-loadable after unload. User are expected to use
bpf_object__close()to unload and free up resources in one operation. No need to exposebpf_object__unload()as a public API, deprecate it.[0] Removebpf_object__unload()insidebpf_object__load_xattr(), it is the caller's responsibility to free up resources, otherwise, the following code path will cause double-free problem when loading failed:
Quoted 5 lines
bpf_prog_load
bpf_prog_load_xattr
bpf_object__load
bpf_object__load_xattr
Did you see this double-free ever happen? I'm looking at the code and not seeing it. Seems like bpf_object__unload() is idempotent, so no mater how many times we call it, it doesn't do any harm. Look at how zclose and zfree are implemented, they zero-out fields and also check for non-zero values before doing something. So unless I'm missing something, there is no problem.
Quoted 16 lines
Replace
bpf_object__unload()insidebpf_object__close()with the necessary cleanup operations to avoid compilation error.[0] Closes: https://github.com/libbpf/libbpf/issues/290
Signed-off-by Hengqi Chen <[email protected]>--- tools/lib/bpf/libbpf.c | 8 +++++--- tools/lib/bpf/libbpf.h | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-)tools/lib/bpf/libbpf.c +0
-0
tools/lib/bpf/libbpf.c int bpf_object__load_xattr(struct bpf_object_load_attr *attr) · line 6934
Quoted 4 lines
tools/lib/bpf/libbpf.c +0
-1@@ -6931,7 +6931,6 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)if (obj->maps[i].pinned && !obj->maps[i].reused)bpf_map__unpin (&obj->maps[i],NULL ); -bpf_object__unload (obj);
I think unloading already loaded bpf programs is bpf_object__load()'s responsibility, so please don't remove this.
tools/lib/bpf/libbpf.c void bpf_object__close(struct bpf_object *obj) · line 7543
Quoted 8 lines
tools/lib/bpf/libbpf.c +0
-1pr_warn ("failed to load object '%s'\n" , obj->path);return libbpf_err (err); }@@ -7540,12 +7539,15 @@ void bpf_object__close(struct bpf_object *obj)bpf_gen__free (obj->gen_loader);bpf_object__elf_finish (obj); -bpf_object__unload (obj);
same, this is fine, don't remove it
tools/lib/bpf/libbpf.c void bpf_object__close(struct bpf_object *obj) · line 7550
Quoted 10 lines
tools/lib/bpf/libbpf.c +5
-1btf__free (obj->btf);btf_ext__free (obj->btf_ext); -for (i =0 ; i < obj->nr_maps; i++) +for (i =0 ; i < obj->nr_maps; i++) { +zclose (obj->maps[i].fd); +if (obj->maps[i].st_ops) +zfree (&obj->maps[i].st_ops->kern_vdata);bpf_map__destroy (&obj->maps[i]); + }
and no changes should be necessary here either
Quoted 8 lines
tools/lib/bpf/libbpf.c +0
-0zfree (&obj->btf_custom_path);zfree (&obj->kconfig);tools/lib/bpf/libbpf.h +0
-0
tools/lib/bpf/libbpf.h struct bpf_object_load_attr { · line 152
Quoted 7 lines
tools/lib/bpf/libbpf.h +2
-1@@ -147,7 +147,8 @@ struct bpf_object_load_attr {/* Load/unload object into/from kernel */ LIBBPF_API int bpf_object__load(struct bpf_object *obj); LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr); -LIBBPF_API int bpf_object__unload(struct bpf_object *obj); +LIBBPF_API LIBBPF_DEPRECATED("bpf_object__unload() is deprecated, use bpf_object__close() instead") +int bpf_object__unload (struct bpf_object *obj);
This is the right change, but let's also keep original bpf_object__unload() logic. I'd recommend renaming bpf_object__unload() into bpf_object_unload() (so that's naming is more clearly showing it's an internal function) and make it static. Then have a small shim of bpf_object__unload() calling into bpf_object_unload() until we remove that in libbpf 1.0.
Quoted 5 lines
tools/lib/bpf/libbpf.h +0
-1LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj); LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj); --2.25.1
Quoted 15 lines · Andrii Nakryiko
Quoted 8 lines · Hengqi Chen
BPF objects are not re-loadable after unload. User are expected to use
bpf_object__close()to unload and free up resources in one operation. No need to exposebpf_object__unload()as a public API, deprecate it.[0] Removebpf_object__unload()insidebpf_object__load_xattr(), it is the caller's responsibility to free up resources, otherwise, the following code path will cause double-free problem when loading failed:Quoted 5 lines
bpf_prog_load
bpf_prog_load_xattr
bpf_object__load
bpf_object__load_xattr
Quoted 8 lines
Did you see this double-free ever happen? I'm looking at the code and not seeing it. Seems like
bpf_object__unload()is idempotent, so no mater how many times we call it, it doesn't do any harm. Look at how zclose and zfree are implemented, they zero-out fields and also check for non-zero values before doing something. So unless I'm missing something, there is no problem.
Sorry, I made a stupid mistake.
Did not realize zclose/zfree are implemented as macros. Will remove these changes.
Quoted 29 lines
Quoted 16 lines
Replace
bpf_object__unload()insidebpf_object__close()with the necessary cleanup operations to avoid compilation error.[0] Closes: https://github.com/libbpf/libbpf/issues/290
Signed-off-by Hengqi Chen <[email protected]>--- tools/lib/bpf/libbpf.c | 8 +++++--- tools/lib/bpf/libbpf.h | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-)tools/lib/bpf/libbpf.c +0
-0tools/lib/bpf/libbpf.c int bpf_object__load_xattr(struct bpf_object_load_attr *attr) · line 6934
Quoted 4 lines
tools/lib/bpf/libbpf.c +0
-1@@ -6931,7 +6931,6 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)if (obj->maps[i].pinned && !obj->maps[i].reused)bpf_map__unpin (&obj->maps[i],NULL ); -bpf_object__unload (obj);I think unloading already loaded bpf programs is bpf_object__load()'s responsibility, so please don't remove this.
Quoted 5 lines
tools/lib/bpf/libbpf.c +0
-0pr_warn ("failed to load object '%s'\n" , obj->path);return libbpf_err (err); }@@ -7540,12 +7539,15 @@ void bpf_object__close(struct bpf_object *obj)
Quoted 6 lines
tools/lib/bpf/libbpf.c void bpf_object__close(struct bpf_object *obj) · line 7543
Quoted 3 lines
tools/lib/bpf/libbpf.c +0
-1bpf_gen__free (obj->gen_loader);bpf_object__elf_finish (obj); -bpf_object__unload (obj);same, this is fine, don't remove it
OK.
Quoted 11 lines
tools/lib/bpf/libbpf.c void bpf_object__close(struct bpf_object *obj) · line 7550
Quoted 10 lines
tools/lib/bpf/libbpf.c +5
-1btf__free (obj->btf);btf_ext__free (obj->btf_ext); -for (i =0 ; i < obj->nr_maps; i++) +for (i =0 ; i < obj->nr_maps; i++) { +zclose (obj->maps[i].fd); +if (obj->maps[i].st_ops) +zfree (&obj->maps[i].st_ops->kern_vdata);bpf_map__destroy (&obj->maps[i]); + }
Quoted 2 lines
and no changes should be necessary here either
Acked.
Quoted 14 lines
Quoted 14 lines
tools/lib/bpf/libbpf.c +0
-0zfree (&obj->btf_custom_path);zfree (&obj->kconfig);tools/lib/bpf/libbpf.h +2
-1@@ -147,7 +147,8 @@ struct bpf_object_load_attr {/* Load/unload object into/from kernel */ LIBBPF_API int bpf_object__load(struct bpf_object *obj); LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr); -LIBBPF_API int bpf_object__unload(struct bpf_object *obj); +LIBBPF_API LIBBPF_DEPRECATED("bpf_object__unload() is deprecated, use bpf_object__close() instead") +int bpf_object__unload (struct bpf_object *obj);
Quoted 9 lines
Quoted 1 line
This is the right change, but let's also keep original
bpf_object__unload()logic. I'd recommend renamingbpf_object__unload()intobpf_object_unload()(so that's naming is more clearly showing it's an internal function) and make it static. Then have a small shim ofbpf_object__unload()calling intobpf_object_unload()until we remove that in libbpf 1.0.
OK, since LIBBPF_DEPRECATED_SINCE is landed, will use it instead.
Quoted 5 lines
Quoted 5 lines
LIBBPF_APIconst char *bpf_object__name(const structbpf_object*obj);LIBBPF_APIunsigned intbpf_object__kversion(const struct bpf_object *obj);
--
2.25.1