Commit 689624f0 authored by Joe Burton's avatar Joe Burton Committed by Andrii Nakryiko

libbpf: Deprecate bpf_objects_list

Add a flag to `enum libbpf_strict_mode' to disable the global
`bpf_objects_list', preventing race conditions when concurrent threads
call bpf_object__open() or bpf_object__close().

bpf_object__next() will return NULL if this option is set.

Callers may achieve the same workflow by tracking bpf_objects in
application code.

  [0] Closes: https://github.com/libbpf/libbpf/issues/293Signed-off-by: default avatarJoe Burton <jevburton@google.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211026223528.413950-1-jevburton.kernel@gmail.com
parent 20d1b54a
...@@ -1148,6 +1148,7 @@ static struct bpf_object *bpf_object__new(const char *path, ...@@ -1148,6 +1148,7 @@ static struct bpf_object *bpf_object__new(const char *path,
size_t obj_buf_sz, size_t obj_buf_sz,
const char *obj_name) const char *obj_name)
{ {
bool strict = (libbpf_mode & LIBBPF_STRICT_NO_OBJECT_LIST);
struct bpf_object *obj; struct bpf_object *obj;
char *end; char *end;
...@@ -1188,6 +1189,7 @@ static struct bpf_object *bpf_object__new(const char *path, ...@@ -1188,6 +1189,7 @@ static struct bpf_object *bpf_object__new(const char *path,
obj->loaded = false; obj->loaded = false;
INIT_LIST_HEAD(&obj->list); INIT_LIST_HEAD(&obj->list);
if (!strict)
list_add(&obj->list, &bpf_objects_list); list_add(&obj->list, &bpf_objects_list);
return obj; return obj;
} }
...@@ -7935,6 +7937,10 @@ struct bpf_object * ...@@ -7935,6 +7937,10 @@ struct bpf_object *
bpf_object__next(struct bpf_object *prev) bpf_object__next(struct bpf_object *prev)
{ {
struct bpf_object *next; struct bpf_object *next;
bool strict = (libbpf_mode & LIBBPF_STRICT_NO_OBJECT_LIST);
if (strict)
return NULL;
if (!prev) if (!prev)
next = list_first_entry(&bpf_objects_list, next = list_first_entry(&bpf_objects_list,
......
...@@ -168,7 +168,8 @@ LIBBPF_API struct bpf_program * ...@@ -168,7 +168,8 @@ LIBBPF_API struct bpf_program *
bpf_object__find_program_by_name(const struct bpf_object *obj, bpf_object__find_program_by_name(const struct bpf_object *obj,
const char *name); const char *name);
LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev); LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 7, "track bpf_objects in application code instead")
struct bpf_object *bpf_object__next(struct bpf_object *prev);
#define bpf_object__for_each_safe(pos, tmp) \ #define bpf_object__for_each_safe(pos, tmp) \
for ((pos) = bpf_object__next(NULL), \ for ((pos) = bpf_object__next(NULL), \
(tmp) = bpf_object__next(pos); \ (tmp) = bpf_object__next(pos); \
......
...@@ -57,6 +57,12 @@ enum libbpf_strict_mode { ...@@ -57,6 +57,12 @@ enum libbpf_strict_mode {
* function name instead of section name. * function name instead of section name.
*/ */
LIBBPF_STRICT_SEC_NAME = 0x04, LIBBPF_STRICT_SEC_NAME = 0x04,
/*
* Disable the global 'bpf_objects_list'. Maintaining this list adds
* a race condition to bpf_object__open() and bpf_object__close().
* Clients can maintain it on their own if it is valuable for them.
*/
LIBBPF_STRICT_NO_OBJECT_LIST = 0x08,
__LIBBPF_STRICT_LAST, __LIBBPF_STRICT_LAST,
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment